Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Years and months between two dates
Message
 
To
22/11/1998 13:16:02
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00160317
Message ID:
00161429
Views:
16
>Does anybody have a quick routine that will calculate the number of years, months and days between two given dates? (Taking into account leap years, millenium, etc.)
>
>Thanks!
>
Hi Peter,

I love problems like these, so even though Sylvain has already provided an answer, I can't resist sharing mine:
* YrsMosDays

LPARAMETERS pddate1, pddate2, pnyears, pnmonths, pndays

LOCAL ldearly, ldlate, ldbefore, lnyears, ldnewdate
ldearly = MIN(pddate1, pddate2)
ldlate = MAX(pddate1, pddate2)
* Create a date made of the later year and
* the month and day of the earlier date
ldbefore = CTOD(ALLTRIM(STR(MONTH(ldearly))) + "/" +;
  ALLTRIM(STR(DAY(ldearly))) + "/" + ;
  ALLTRIM(STR(YEAR(ldlate))))
* Calculate years and adjust if necessary
pnyears = YEAR(ldlate) - YEAR(ldearly)
IF ldbefore > ldlate
  pnyears = pnyears - 1
ENDIF
* Adjust the earlier date to be before the later
ldnewdate = GOMONTH(ldearly, pnyears * 12)
* Calculate months and adjust if necessary
pnmonths = MONTH(ldlate) - MONTH(ldnewdate)
IF pnmonths < 0
  pnmonths = pnmonths + 12
ELSE
  IF ldbefore > ldlate AND pnmonths = 0
    pnmonths = 11
  ENDIF
ENDIF
* Adjust the date so it's one month before or less
ldnewdate = GOMONTH(ldnewdate, pnmonths)
* Subtract
pndays = ldlate - ldnewdate
RETURN
In the above no consideration is made for either the current status of SET DATE or STRICTDATE.
George

Ubi caritas et amor, deus ibi est
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform