How do I determine the First and Last
Sundays of a Month?Keywords: timejulianday timeymdhms timeadd
This example determines if a day is the first or last sunday of a month and can also tell if another day is before the first sunday, in between or after the last sundaytodate=TimeYmdHms() ;todate=AskLine("Debug","Enter a date",todate) jultodate=TimeJulianDay(todate) dayofweek=(jultodate+5) mod 7 ; at this point if dayofweek==0 then it is Sunday dayofweek==6 is saturday year=ItemExtract(1,todate,":") Month=ItemExtract(2,todate,":") Day=ItemExtract(3,todate,":") ;now figure out last day of month ldom=31 if (month==9 || month==4 || month==6 || month==11) then ldom=30 if month==2 ldom=28 if ((year mod 4)==0) then ldom=29 endif ; firstdateofmonth="%year%:%month%:01:00:00:00" julfirst=TimeJulianDay(firstdateofmonth) firstday=(julfirst+5) mod 7 if firstday!=0 ; fitst of month is not sunday delta=7-firstday firstsundate=TimeAdd(firstdateofmonth,"00:00:%delta%:00:00:00") else firstsundate=firstdateofmonth endif ;Message("First Sunday is",firstsundate) ; now nail down last sunday of the month lastdateofmonth="%year%:%month%:%ldom%:00:00:00" jullast=TimeJulianDay(lastdateofmonth) lastday=(jullast+5) mod 7 if lastday!=0 ; fitst of month is not sunday lastsundate=TimeSubtract(lastdateofmonth,"00:00:%lastday%:00:00:00") else lastsundate=lastdateofmonth endif ;Message("Last Sunday is",lastsundate) ;Now that we (finally) figured out first and last sunday... ;where are we now ;debug(1) checkfirst=TimeDiffDays(firstsundate,todate) if checkfirst>0 then dateflag=0 if checkfirst==0 then dateflag=1 if checkfirst<0 checklast=TimeDiffDays(lastsundate,todate) if checklast>0 then dateflag=2 if checklast==0 then dateflag=3 if checklast<0 then dateflag=4 endif if dateflag==0 then Message(todate,"Today is BEFORE the first Sunday") if dateflag==1 then Message(todate,"Today is the FIRST Sunday") if dateflag==2 then Message(todate,"Today is BETWEEN the first and last Sunday") if dateflag==3 then Message(todate,"Today is the LAST Sunday") if dateflag==4 then Message(todate,"Today is AFTER the last Sunday")How to Modify the Above Code, to Determine first Tuesday
Again, keep in mind the following is returned from the dayofweek variable below: Su=0, M=1, T=2,W=3, Th=4, F=5, Sa=6. So, if firstday does not return a "2", we know that the first day of the month is not a Tuesday. Therefore to determine the first Tuesday, we can subtract the firstday from "9", which we get from adding the day of the week which is Tuesday (2) plus the number of days in a week (7) = 9. We use this to get delta below.Accordingly, if you wanted to get a Sunday: 7-firstday (0+7); Monday: delta=8-firstday; Wednesday: delta=10-firstday; Thurday: delta=11-firstday; Friday: 12-firstday; Saturday: 13-firstday; .
todate=TimeYmdHms() ;todate=AskLine("Debug","Enter a date",todate) jultodate=TimeJulianDay(todate) dayofweek=(jultodate+5) mod 7 ; at this point if dayofweek==0 then it is Sunday dayofweek==6 is saturday year=ItemExtract(1,todate,":") Month=ItemExtract(2,todate,":") Day=ItemExtract(3,todate,":") ;now figure out last day of month ldom=31 if (month==9 || month==4 || month==6 || month==11) then ldom=30 if month==2 ldom=28 if ((year mod 4)==0) then ldom=29 endif ; firstdateofmonth="%year%:%month%:01:00:00:00" julfirst=TimeJulianDay(firstdateofmonth) firstday=(julfirst+5) mod 7 message(1,firstday) ;firstday=11-firstday ;11 is from Tuesday, which is 4, plus 7 if firstday!=2 ; first of month is not tuesday ::<<<< Su=0,M=1,T=2,W=3,Th=4,F=5,Sa=6 delta=9-firstday ;9=2+7 ::<<<< message(1,delta) firsttuesday=TimeAdd(firstdateofmonth,"00:00:%delta%:00:00:00") else firsttuesday=firstdateofmonth endif Message("First Tuesday is",firsttuesday)
Article ID: W13884Filename: Time - First Sunday, Last Sunday of Month.txt