Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dates - doing year arithmetic

Status
Not open for further replies.

jimstarr

Programmer
Feb 6, 2001
975
US
When doing arithmetic with dates in VFP the solutions are usually pretty obvious when it comes to days and months:

Add 5 days to today's date:
newdate = DATE() + 5

Subtract 162 days from today's date:
newdate = DATE() - 162

Add 7 months to today's date:
newdate = GOMONTH(DATE(), 7)

Subtract 2 months from today's date:
newdate = GOMONTH(DATE(), -2)

But years are different. It would be nice to have a GOYEAR() function to do year arithmetic directly, but it isn't in the language (last time I looked!). So programmers often get the job done by manipulating the year portion of a date using combinations of DTOC(), YEAR(), VAL(), SUBSTR() and CTOD().

Though not exactly intuitive, I've found it much easier to use GOMONTH():

Add 1 year to today's date:
newdate = GOMONTH(DATE(), 12)
Subtract 3 years from today's date:
newdate = GOMONTH(DATE(), -36)

In fact, I'm guessing that the lack of a GOYEAR() function may be because the VFP developers consider it unnecessary.

So whenever I find myself struggling with a date arithmetic problem I try to remember the very useful GOMONTH().

Jim
 
This is nice, but you'll need to compensate for leap years.

Darrell

'We all must do the hard bits so when we get bit we know where to bite' :)
 
Or solve your y2k problem:
newdate = GOMONTH(olddate, 1200)
 
It seems to me that GOMONTH() does compensate for leap years, and has no problem with Y2K issues. I'd be interested to know if I'm wrong about this. Please provide examples. Thanks!

Jim
 
Y2K issues have been resolved since VFP5. The SET CENTURY TO nCentury ROLLOVER nYear command helped fixed it without the use of SET CENTURY ON.

Medic
 
Jim,

I have always used GOMONTH() for year arithmetic. In fact, it never occurred to me to do it any other way. And, yes, GOMONTH() does know about leap years.

However, there is one quirk in GOMONTH(). It doesn't work with dates earlier than September 2, 1752, which happens to be the day on which the Gregorian calendar was introduced into England (not Scotland) and its dominions, which of course included those 13 colonies across the Atlantic.

For some background on this, see my article, "Take care with dates and times", at:

Mike


Mike Lewis
Edinburgh, Scotland
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top