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
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