Thanks to all for your help. My son even chipped in with a mathematical
approach which actually works, but I settled on this:
(start_year * 12) - (end_year * 12) + (start_month - end_month)
If the start_month value is smaller than the end_month, the result will be
negative, and adding it to the years will still produce the right numbers.
Then doing an ABS() will give the months without regard to starting/ending
dates.
Here's code:
001 10 CRT 'ENTER START DATE ':
002 INPUT BEG.DATE
003 IF BEG.DATE = '' THEN STOP
004 BEG.DATE = ICONV(BEG.DATE,'D')
005 CRT 'ENTER END DATE ':
006 INPUT END.DATE
007 END.DATE = ICONV(END.DATE,'D')
008 BEG.YEAR = OCONV(BEG.DATE,'D4/')[7,4]
009 END.YEAR = OCONV(END.DATE,'D4/')[7,4]
010 BEG.MONTH = OCONV(BEG.DATE,'D2/')[1,2]
011 END.MONTH = OCONV(END.DATE,'D2/')[1,2]
012 MONTHS = ABS(((BEG.YEAR - END.YEAR) * 12) + (BEG.MONTH - END.MONTH))
013 CRT 'MONTHS = ':MONTHS
014 CRT
015 GO 10
Ed
|
|
|
| > Someone asked me if there was an easy way to calculate elapsed months
from
| > two dates. My first response was, "Sure, that's easy." Then I proceeded
to
| > prove myself wrong. Maybe it's Monday brain fade, but the answer is
eluding
| > me.
| >
| > I've tried this:
| >
| > 200103 (Mar 2000)
| > - 199611 (Nov 1996)
| > --------
| > 492
| >
| > Obviously wrong. The answer in this case is 52 months isn't it? I've
never
| > had to do this calc before. The answer must be accurate to the day, so
| > approximating won't work. If the start date is near a month border, the
calc
| > must detect it.
| >
| > Thanks, group.
| >
| > Ed
|
| I don't know of any easy way but here is a method that should work.
|
| 1. Multiply the 4 digit year by 12.
|
| 2. Add the number of the month to the results of (1)
|
| 3. Do this for both start and stop dates.
|
| 4. Subtract the start number from the stop number. If the stop day is less
than
| the start day you may want to subtract 1 from the results.
|
| Hope this helps.
|