Number of months between 2 dates

Pickering, John (NORBORD) PICKERIJ@norbord.com
Thu, 4 Sep 2003 13:32:35 -0400


Terry

OK. I said messier, not harder :-)

You'll need to break the start and end dates apart into yyyy and mm:

>define d-start-yyyy int*4 = floor(d-start-date / 10000)
>define d-end-yyyy int*4 = floor(d-end-date / 10000)

>define d-start-mm int*2 = floor(mod(d-start-date,10000) / 100)
>define d-end-mm int*2 = floor(mod(d-end-date,10000) / 100)

Calculate the number of whole years:

>define d-whole-years = d-end-yyyy - d-start-yyyy - 1

Now add up the months:

>define d-total-months int*5 &
> = (d-whole-months * 12) & ; each whole year is worth 12 months
> + (12 - d-start-mm) & ; number of whole months from start month to Dec
> + (d-end-mm - 1)   ; number of whole months from Jan to end month

Warnings:
- above code is untested
- assumes start and end dates are stored sensibly in yyyymmdd format
- assumes partial months at start and end don't count 
- may be incorrect if your start or end dates are the last day of the month
-- but you can fix this by comparing your date with lastday(your date) and
adjusting the last two lines of the last define (i.e. make the constants
into defines) [just ask if you need help with this]

Hope this helps.

Regards,
JWP



-----Original Message-----
From: Terry Pickering [mailto:pickering@4j.lane.edu]
Sent: Thursday, September 04, 2003 1:00 PM
To: Pickering, John (NORBORD)
Subject: RE: Number of months between 2 dates


Unfortunately it's "actual"  whole calendar months, whether they be 28 day, 
30, or 31. The dates are mmddyyyy. This will be used to compute retirement 
benefits which are based upon the number of months between retirement and 
age 65, which could be up to 15 years.



At 12:46 PM 9/4/2003 -0400, you wrote:
>Terry
>
>It depends on what you mean by a "month". If you'd be happy with how many
30
>day chunks there are then something like the following should do it:
>
> >define d-months int*5 &
> >  = floor( (days(to-date) - days(from-date)) / 30)
>
>If you want to know how many whole calendar months then the problem gets a
>whole lot messier. And we'd need to know if your dates are mmddyyyy or
>ddmmyyyy!
>
>Regards,
>John "no relation" Pickering
>
>-----Original Message-----
>From: Terry Pickering [mailto:pickering@4j.lane.edu]
>Sent: Thursday, September 04, 2003 12:17 PM
>To: powerh-l@lists.swau.edu
>Subject: Number of months between 2 dates
>
>
>Does anyone have an algorithm around that would compute the number of full
>MONTHS between two dates? Example is the number of months between 7/12/1968
>and 10/8/2002.
>
>Thanks!
>
>
>
>____________________________
>Terry Pickering

____________________________
Terry Pickering