Find the last Sunday of each month: Difference between revisions

m (→‎{{header|Sidef}}: modified the code to work with Sidef 2.30)
Line 1,905:
24 November
29 December</pre>
 
=={{header|Liberty BASIC}}==
This program goes a step further and provides a function definition, NthDayOfMonth(), that returns the date of any occurrence of a day in any given month and year. For example: The third Monday in February, the last Monday in May, the fourth Thursday in November, etc.
<lang lb>
yyyy=2013: if yyyy<1901 or yyyy>2099 then end
nda$="Lsu"
print "The last Sundays of "; yyyy
for mm=1 to 12
x=NthDayOfMonth(yyyy, mm, nda$)
select case mm
case 1: print " January "; x
case 2: print " February "; x
case 3: print " March "; x
case 4: print " April "; x
case 5: print " May "; x
case 6: print " June "; x
case 7: print " July "; x
case 8: print " August "; x
case 9: print " September "; x
case 10: print " October "; x
case 11: print " November "; x
case 12: print " December "; x
end select
next mm
end
 
function NthDayOfMonth(yyyy, mm, nda$)
' nda$ is a two-part code. The first character, n, denotes
' first, second, third, fourth, and last by 1, 2, 3, 4, or L.
' The last two characters, da, denote the day of the week by
' mo, tu, we, th, fr, sa, or su. For example:
' the nda$ for the second Monday of a month is "2mo";
' the nda$ for the last Thursday of a month is "Lth".
if yyyy<1900 or yyyy>2099 or mm<1 or mm>12 then
NthDayOfMonth=0: exit function
end if
nda$=lower$(trim$(nda$))
if len(nda$)<>3 then NthDayOfMonth=0: exit function
n$=left$(nda$,1): nC$="1234l"
da$=right$(nda$,2): daC$="tuwethfrsasumotuwethfrsasumo"
if not(instr(nC$,n$)) or not(instr(daC$,da$)) then
NthDayOfMonth=0: exit function
end if
NthDayOfMonth=1
mm$=str$(mm): if mm<10 then mm$="0"+mm$
db$=DayOfDate$(str$(yyyy)+mm$+"01")
if da$<>db$ then
x=instr(daC$,db$): y=instr(daC$,da$,x): NthDayOfMonth=1+(y-x)/2
end if
dim MD(12)
MD(1)=31: MD(2)=28: MD(3)=31: MD(4)=30: MD(5)=31: MD(6)=30
MD(7)=31: MD(8)=31: MD(9)=30: MD(10)=31: MD(11)=30: MD(12)=31
if yyyy mod 4 = 0 and yyyy<>2000 then MD(2)=29
if n$<>"1" then
if n$<>"l" then
NthDayOfMonth=NthDayOfMonth+((val(n$)-1)*7)
else
if NthDayOfMonth+27<MD(mm) then
NthDayOfMonth=NthDayOfMonth+28
else
NthDayOfMonth=NthDayOfMonth+21
end if
end if
end if
end function
 
function DayOfDate$(ObjectDate$) 'yyyymmdd format
if ObjectDate$="" then 'today
DaysSince1900 = date$("days")
else
DaysSince1900 = date$(mid$(ObjectDate$,5,2)+"/"+right$(ObjectDate$,2)_
+"/"+left$(ObjectDate$,4))
end if
DayOfWeek = DaysSince1900 mod 7
select case DayOfWeek
case 0: DayOfDate$="tu"
case 1: DayOfDate$="we"
case 2: DayOfDate$="th"
case 3: DayOfDate$="fr"
case 4: DayOfDate$="sa"
case 5: DayOfDate$="su"
case 6: DayOfDate$="mo"
end select
end function
</lang>
{{out}}
<pre>
The last Sundays of 2013
January 27
February 24
March 31
April 28
May 26
June 30
July 28
August 25
September 29
October 27
November 24
December 29
</pre>
 
=={{header|LiveCode}}==
Anonymous user