GoMonth()

This function returns a date offset from the date or datetime supplied by the number of months indicated (positive or negative). It is limited to dates in the year 1753 or later, returning empty dates if that lower limit is exceeded. Watch out for the type change this function causes: GOMONTH() always returns a date, whether supplied with a date or a datetime.

Usage

dReturn = GOMONTH( dDate | tDateTime, nOffset )
Have you ever wondered how to keep track of Aunt Tilly's birthday? Without fail, I always think it's a May date in April and an April date in May. And which birthday is it, anyway? Is she 60? 75? 142? Well, the answer, of course, is to keep track of Tilly's date of birth in a database, specifically in a date field, and have your machine remind you when her birthday approaches. How do you know when her birthday is coming up? Date math, of course! Just subtract the date from today, and you'll get, er, 26,263 days. Nope, that's not right—because FoxPro calculates the entire date, including the year amount. What you'll need to do is to compare the dates after shifting them into the same year and sound an alarm when the date's within a week or two. An excellent application for the GOMONTH() function, as demonstrated in the DiffDate function below:
* DiffDate.PRG
* Returns the absolute difference in number of days between 2 dates,
* ignoring year, i.e., 9/8/43 vs. 9/9/94 returns 1 and 
* 7/6/12 vs. 7/5/83 returns 1.
LPARAMETERS tdDate1, tdDate2
LOCAL ldNewDate, lnDifference, lnYears
* Validate parameters, force to today's date if blank
tdDate1 = MakeItDate(tdDate1)
tdDate2 = MakeItDate(tdDate2)
* Use GOMONTH() function to bring dates into the same year, then
* subtract the difference.
lnYears = YEAR(tdDate2) - YEAR(tdDate1)
ldNewDate = GOMONTH(tdDate1, lnYears * 12)
lnDifference = tdDate2 – ldNewDate
* If they're still too far apart, bump ldNewDate a year + / -
IF ABS(lnDifference) > 182
  lnYears = lnYears + 1*SIGN(lnDifference)
  ldNewDate = GOMONTH(tdDate1, lnYears * 12)
  lnDifference = tdDate2 – ldNewDate
ENDIF
RETURN ABS(lnDifference)
 
FUNCTION MakeItDate (tdDate2Test)
* Validate the parameter as a legitimate date or 
* default to today's date instead.
RETURN IIF(EMPTY(tdDate2Test) OR ;
           NOT INLIST(TYPE("tdDate2Test"),"D","T"), ;
           DATE(), ;
           tdDate2Test)

Example

? GOMONTH({06/15/1958},70*12)  && Returns "06/15/2028"
* A favorite of Doug, to calculate the 
* last day of the current month.
ldLastDay = GOMONTH(DATE() - DAY(DATE()) + 1, 1) – 1

See Also

CDoW(), CMonth(), Date(), Day(), DMY(), DoW(), MDY(), Month(), Set Century, Set Date, Set Mark To, Year()


Back to Table of Contents

Copyright © 2002-2018 by Tamar E. Granor, Ted Roche, Doug Hennig, and Della Martin. Click for license .