This FirstDayOfQuarter and siblings is much easier to do:
******************************************* FUNCTION GoQuarter(dDate, cValue) * "L" = (L)ast day of dDate's Quarter * "F" = (F)irst day of dDate's Quarter * "N" = first day of (n)ext dDate's Quarter * "P" = first day of (p)previous dDate's Quarter *******************************************
cValue = EVL(cValue, "F") LOCAL dResult DO CASE CASE cValue = "L" dResult = GOMONTH(dDate,(CEILING(MONTH(dDate)/3)*3)-MONTH(dDate)+1)-DAY(dDate) CASE cValue = "F" dResult = GOMONTH(dDate,(CEILING(MONTH(dDate)/3)*3)-MONTH(dDate)-2)-DAY(dDate)+1 CASE cValue = "N" dResult = GOMONTH(dDate,(CEILING(MONTH(dDate)/3)*3)-MONTH(dDate)+1)-DAY(dDate)+1 CASE cValue = "P" dResult = GOMONTH(dDate,(CEILING(MONTH(dDate)/3)*3)-MONTH(dDate)-5)-DAY(dDate)+1 ENDCASE RETURN dResult
wOOdy
"*´¨) ¸.·´¸.·*´¨) ¸.·*¨) (¸.·´. (¸.·` * .·`.Visual FoxPro: It's magic ! (¸.·``··*
-----Ursprüngliche Nachricht----- Von: ProFox profox-bounces@leafe.com Im Auftrag von Frank Cazabon Gesendet: Freitag, 18. Mai 2018 18:41 An: profox@leafe.com Betreff: Re: Fun with date calculations in VFP
Lots of spare time today so some hopefully constructive criticism: :)
I would redo some of that code to get away from CTOD() as that will fail depending on SET STRICTDATE and if SET DATE is anything besides MDY.
I have a feeling that your first day of week and last day of week will be incorrect in situations with SET FDOW.
I think PARAMETERS() is also advised against and PCOUNT() is better.
I would also move each function into its own program.
For example FirstDayOfQuarter.prg would be:
LPARAMETERS tdDate
LOCAL lnMonth as Integer, lnYear as Integer, ldDate as Date if PCOUNT()=0 then m.tdDate = date() endif m.lnYear = YEAR(m.tdDate) m.lnMonth = MONTH(m.tdDate) DO CASE CASE BETWEEN(m.lnMonth,1,3) m.ldDate = DATE(m.lnYear, 1, 1) CASE BETWEEN(m.lnMonth,4,6) m.ldDate = DATE(m.lnYear, 4, 1) CASE BETWEEN(m.lnMonth,7,9) m.ldDate = DATE(m.lnYear, 7, 1) OTHERWISE && CASE BETWEEN(m.lnMonth,10,12) m.ldDate = DATE(m.lnYear, 10, 1) ENDCASE return ldDate
Frank.