Calendars and dates
Dates are harder than they look — months of different length, leap years, weekends. This lesson builds a text calendar, an age calculator and a business-day counter, using what .NET already knows.
What you will learn#
DaysInMonth,DayOfWeek,DayOfYear,- adding months and years safely,
- computing an age, business days and the next birthday,
SubtractandTimeSpan.
Why not do it yourself#
Imagine adding one month to 31 January by hand. Is it 31 February? Does the answer depend on the year? DateTime knows all of this. The rule of this lesson: let DateTime do the calendar arithmetic; you only decide what to ask.
The program#
# calendars - month tables, ages, business days and birthdays with DateTime
load "dotnet.ring"
using {
"System"
}
func main
# 1. A text calendar of a month: how many days it has, and on which weekday the 1st falls
? monthCalendar(2024,2)
# 2. How old is somebody? Years, months and days between two dates
? "Age of a person born on 1990-03-15, on 2024-03-14: " + ageText(1990,3,15,2024,3,14)
? " and on 2024-03-15 : " + ageText(1990,3,15,2024,3,15)
# 3. Days between dates, and business days (Monday to Friday)
oFrom = new DateTime(2024,12,20)
oTo = new DateTime(2025,1,6)
? nl + "From 2024-12-20 to 2025-01-06: " + oTo.Subtract(oFrom).Days + " days, " + businessDays(oFrom,oTo) + " business days"
# 4. The next birthday: this year's date, or next year's when it has passed
? nl + "Days until the next birthday (3 March), counted from 2024-05-10: " + daysUntilBirthday(3,3,2024,5,10)
? " counted from 2024-02-01: " + daysUntilBirthday(3,3,2024,2,1)
# 5. Adding to a date: months of different length, and the end of the month
oStart = new DateTime(2024,1,31)
? nl + "2024-01-31 plus one month : " + oStart.AddMonths(1).Format("yyyy-MM-dd") + " (February has no 31st: the last day is used)"
? " plus one year (leap day) : " + new DateTime(2024,2,29).AddYears(1).Format("yyyy-MM-dd")
? " plus 100 days : " + oStart.AddDays(100).Format("yyyy-MM-dd")
# 6. The week number and the day of the year
oDate = new DateTime(2024,12,25)
? nl + "2024-12-25 is day " + oDate.DayOfYear + " of the year, a " + oDate.DayOfWeek
# A month as text: a title, the weekday names, then the days in rows of seven (the week starts on Monday)
func monthCalendar nYear, nMonth
oFirst = new DateTime(nYear,nMonth,1)
nDays = new DateTime(nYear,nMonth,1).DaysInMonth(nYear,nMonth)
cText = "" + oFirst.Format("MMMM yyyy") + " (" + nDays + " days)" + nl
cText += " Mo Tu We Th Fr Sa Su" + nl + " "
# the column of the first day: Monday = 0 ... Sunday = 6
aWeekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
nColumn = find(aWeekdays,oFirst.DayOfWeek) - 1
cText += copies(" ",nColumn)
for nDay = 1 to nDays
cText += right(" " + nDay,2) + " "
nColumn++
if nColumn = 7 and nDay < nDays
cText += nl + " "
nColumn = 0
ok
next
return cText
func copies cText, nTimes
cResult = ""
for nIndex = 1 to nTimes
cResult += cText
next
return cResult
# complete years, months and days between a birth date and a date
func ageText nBirthYear, nBirthMonth, nBirthDay, nYear, nMonth, nDay
nYears = nYear - nBirthYear
nMonths = nMonth - nBirthMonth
nDays = nDay - nBirthDay
if nDays < 0
nMonths--
# the days of the month before the date
nDays += new DateTime(nYear,nMonth,1).AddMonths(-1).DaysInMonth(new DateTime(nYear,nMonth,1).AddMonths(-1).Year,new DateTime(nYear,nMonth,1).AddMonths(-1).Month)
ok
if nMonths < 0
nYears--
nMonths += 12
ok
return "" + nYears + " years, " + nMonths + " months, " + nDays + " days"
# the days from oFrom up to (not including) oTo that are not Saturday or Sunday
func businessDays oFrom, oTo
nCount = 0
oDay = oFrom
while oDay.CompareTo(oTo) < 0
cName = oDay.DayOfWeek
if cName != "Saturday" and cName != "Sunday"
nCount++
ok
oDay = oDay.AddDays(1)
end
return nCount
func daysUntilBirthday nMonth, nDay, nYear, nTodayMonth, nTodayDay
oToday = new DateTime(nYear,nTodayMonth,nTodayDay)
oBirthday = new DateTime(nYear,nMonth,nDay)
if oBirthday.CompareTo(oToday) < 0
oBirthday = oBirthday.AddYears(1)
ok
return oBirthday.Subtract(oToday).DaysFebruary 2024 (29 days)
Mo Tu We Th Fr Sa Su
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29
Age of a person born on 1990-03-15, on 2024-03-14: 33 years, 11 months, 28 days
and on 2024-03-15 : 34 years, 0 months, 0 days
From 2024-12-20 to 2025-01-06: 17 days, 11 business days
Days until the next birthday (3 March), counted from 2024-05-10: 297
counted from 2024-02-01: 31
2024-01-31 plus one month : 2024-02-29 (February has no 31st: the last day is used)
plus one year (leap day) : 2025-02-28
plus 100 days : 2024-05-10
2024-12-25 is day 360 of the year, a WednesdayHow it works#
The month calendar#
DaysInMonth(year, month) gives the number of days (29 for February 2024). DayOfWeek of the first day tells in which column it starts: the week starts on Monday, so the column number is the position of the weekday name in the list Monday ... Sunday, minus one. The program then prints the days with right(" " + day,2) so that the numbers line up, and starts a new line after the seventh column.
Age#
An age is complete years, months and days. The simple subtraction year - birthYear is wrong before the birthday. The method:
- subtract years, months and days separately,
- if the days are negative, borrow the length of the previous month and take one month away,
- if the months are negative, borrow 12 months and take one year away.
On 2024-03-14 a person born on 1990-03-15 is 33 years, 11 months, 28 days (28 because February 2024 has 29 days); one day later exactly 34 years.
Business days#
A while loop visits every day from the start up to (not including) the end and counts the days that are not Saturday or Sunday. CompareTo is negative while the first date is earlier. Between 20 December 2024 and 6 January 2025 there are 17 days and 11 business days. (Holidays are not weekends: a real program has a list of holiday dates and skips those too.)
The next birthday#
Build this year's birthday; if it is already past (CompareTo negative), add one year. Subtract(today).Days is the number of days to wait.
Safe additions#
| Call | Result | Why |
|---|---|---|
AddMonths(1) on 2024-01-31 | 2024-02-29 | February has no 31st: the last day is used |
AddYears(1) on 2024-02-29 | 2025-02-28 | 2025 has no leap day |
AddDays(100) | 2024-05-10 | days are simple |
Always use these methods, never AddDays(30) for a month.
Day of the year#
DayOfYear counts from 1 on 1 January: 25 December 2024 is day 360 (2024 is a leap year).
What you learned#
- let
DateTimedo the calendar work, - ages need borrowing,
- business days need a loop and a weekday check,
- additions clamp to the end of the month.
Exercises#
- Print the calendar of the current month (
DateTime.Now) and mark today with[ ]. - Find the next Friday the 13th after today.
- Add a list of holidays to
businessDaysand skip them. - Write
isLeapYear(year)by your own rule and test it againstDateTime.IsLeapYear.