GOMONTH() just moves back to 2/28 on years that don't have a 2/29, so your olddate({^2016-02-29},-2) gives you the previous Tuesday, 2/24/2014.
Hmmm. That's an OBO. It goes back 4 instead of forward 3. I think the nearest date ought to be 2014-03-03 Ah, if thediff = -4, it remains -4. That's a bug. Add the second line below after the first: m.theOffset=IIF(theDiff>3, 7-m.theDiff, m.theDiff) m.theOffset=IIF(theDiff<-3, 7+m.theDiff, m.theDiff) I know there's a better algorithm, likely involving MOD() or CEILING() or FLOOR() but it's escaping me at the moment. There's 49 combinations of original date = (1,2,3,4,5,6,7) and resultant date = (1,2,3,4,5,6,7) and the offset should come out as {-3,-2,-1,0,1,2,3} , but I'm drawing a blank. Anyone? Beuller? On Tue, May 8, 2018 at 2:15 PM, Richard Kaye <rk...@invaluable.com> wrote: > Ted's solution works just fine. What I didn't test yet is using leap day > itself as the starting point. Hmmm..... > > ************************************** > * Program: datestuff3.prg > * Date: 05/08/2018 11:09 AM > * VFP Version: Visual FoxPro 09.00.0000.7423 for Windows > * Notes: > ************************************** > ACTIVATE SCREEN > CLEAR > ? [Today: ]+CHR(9)+CHR(9) > ?? DATE() > ?? CHR(9)+CDOW(DATE()) > ? [Same day last year: ] > ?? olddate(DATE(),-1) > ?? CHR(9)+CDOW(olddate(DATE(),-1)) > ? [Same day 2 years ago: ] > ?? olddate(DATE(),-2) > ?? CHR(9)+CDOW(olddate(DATE(),-2)) > ? [Same day 3 years ago: ] > ?? olddate(DATE(),-3) > ?? CHR(9)+CDOW(olddate(DATE(),-3)) > ? [Same day 10 years ago: ] > ?? olddate(DATE(),-10) > ?? [ ]+CDOW(olddate(DATE(),-10)) > ? [Yesterday: ]+CHR(9) > ?? DATE()-1 > ?? CHR(9)+CDOW(DATE()-1) > ? [Same day last year: ] > ?? olddate(DATE()-1,-1) > ?? CHR(9)+CDOW(olddate(DATE()-1,-1)) > ? [Same day 2 years ago: ] > ?? olddate(DATE()-1,-2) > ?? CHR(9)+CDOW(olddate(DATE()-1,-2)) > ? [Same day 3 years ago: ] > ?? olddate(DATE()-1,-3) > ?? CHR(9)+CDOW(olddate(DATE()-1,-3)) > ? [Same day 10 years ago: ] > ?? olddate(DATE()-1,-10) > ?? [ ]+CDOW(olddate(DATE()-1,-10)) > ? [Tomorrow: ]+CHR(9) > ?? DATE()+1 > ?? CHR(9)+CDOW(DATE()+1) > ? [Same day last year: ] > ?? olddate(DATE()+1,-1) > ?? CHR(9)+CDOW(olddate(DATE()+1,-1)) > ? [Same day 2 years ago: ] > ?? olddate(DATE()+1,-2) > ?? CHR(9)+CDOW(olddate(DATE()+1,-2)) > ? [Same day 3 years ago: ] > ?? olddate(DATE()+1,-3) > ?? CHR(9)+CDOW(olddate(DATE()+1,-3)) > ? [Same day 10 years ago: ] > ?? olddate(DATE()+1,-10) > ?? [ ]+CDOW(olddate(DATE()+1,-10)) > > FUNCTION oldDate(theDate AS Date, offset AS Integer) > * compliments of Ted Roche > * parameters: thedate: date to start from > * offset: number of years offset, positive or negative > * returns: nearest date to the offset that falls on the same day of the week > LOCAL m.theDiff, m.theOffset > m.theDiff=DOW(m.theDate)-DOW(GOMONTH(m.theDate, m.offset*12)) > m.theOffset=IIF(theDiff>3, 7-m.theDiff, m.theDiff) > RETURN GOMONTH(m.theDate, m.offset*12) + m.theOffset > > ENDFUNC > > -- > > rk > > -----Original Message----- > From: ProfoxTech <profoxtech-boun...@leafe.com> On Behalf Of Gene Wirchenko > Sent: Tuesday, May 08, 2018 12:46 PM > To: profoxt...@leafe.com > Subject: RE: Fun with date calculations in VFP > > At 06:27 2018-05-08, Richard Kaye <rk...@invaluable.com> wrote: >>The latter. And that was the approach I was just working through. > > What about the week every few years that you will be disregarding? > > A year has 52 weeks plus one or two days. > > [snip] > > Sincerely, > > Gene Wirchenko > > [excessive quoting removed by server] _______________________________________________ Post Messages to: ProFox@leafe.com Subscription Maintenance: http://mail.leafe.com/mailman/listinfo/profox OT-free version of this list: http://mail.leafe.com/mailman/listinfo/profoxtech Searchable Archive: http://leafe.com/archives/search/profox This message: http://leafe.com/archives/byMID/profox/CACW6n4uxE3UCFQwf3H_9uznN=p1e_yx63bdrcmbz52yjpx_...@mail.gmail.com ** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.