Find the last Sunday of each month: Difference between revisions

Line 3,618:
<pre>
C:\>LASTSQB1 2013
Last Sundays of each month on year: 2013
2013-01-27
2013-02-24
2013-03-31
2013-04-28
2013-05-26
2013-06-30
2013-07-28
2013-08-25
2013-09-29
2013-10-27
2013-11-24
2013-12-29
</pre>
 
==={{header|VB-DOS 1.0}}===
As VB-DOS uses serial numbers for dates and includes some useful functions to work with dates, it is easier to do this code in VB-DOS.
<lang QBASIC>
OPTION EXPLICIT
 
' PROGRAM Last Sundays in VB-DOS 1.0 (LASTVD1)
' This program will calculate the last Sundays of each month in a given year.
' It works assigning the year in the command prompt.
' Usage: LASTSVD1 Year
' In the IDE, be sure to assign the COMMAND$ value in the Run menu.
 
' Var
DIM iWY AS INTEGER
DIM A AS STRING
DIM iWM AS INTEGER
DIM iWD AS INTEGER
 
' SUBs and FUNCTIONs
DECLARE FUNCTION LastSundayOfMonth (WhichMonth AS INTEGER, WhichYear AS INTEGER) AS INTEGER
 
' Initialize
iWY = VAL(COMMAND$)
 
IF iWY >= 1753 AND iWY <= 2078 THEN
PRINT "Last Sundays of each month on year:"; iWY
FOR iWM = 1 TO 12
iWD = LastSundayOfMonth(iWM, iWY)
PRINT FORMAT$(DATESERIAL(iWY, iWM, iWD), "yyyy-mm-dd")
NEXT iWM
ELSE
PRINT "Incorrect year. Please, specify a value between 1753 and 2078."
END IF
 
PRINT
PRINT "End of program"
END
 
FUNCTION LastSundayOfMonth (WhichMonth AS INTEGER, WhichYear AS INTEGER) AS INTEGER
' Var
DIM iLSoM AS INTEGER
 
SELECT CASE WhichMonth
CASE 1, 3, 5, 7, 8, 10, 12
iLSoM = 31
CASE 2
iLSoM = 28 + ABS((WhichYear MOD 4 = 0 AND WhichYear MOD 100 OR WhichYear MOD 400 = 0) <> 0)
CASE ELSE
iLSoM = 30
END SELECT
 
' Get last Sunday
iLSoM = iLSoM - (WEEKDAY(DATESERIAL(WhichYear, WhichMonth, iLSoM)) - 1)
 
LastSundayOfMonth = iLSoM
END FUNCTION
</lang>
 
{{out}}
<pre>
C:\>LASTSVD1 2013
Last Sundays of each month on year: 2013
2013-01-27
58

edits