Working days
Business programs count days that are not weekends and not holidays, group dates into weeks and quarters, and compute ages. This lesson adds working days to a date, counts them between two dates, finds ISO week numbers and the end of a month, and computes an age.
What you will learn#
DayOfWeekand a weekend that is kept in one place,- adding working days: a loop over
AddDays(1)that skips weekends and holidays, - counting working days between two dates,
- ISO week numbers and why week 1 can start in December,
- quarters and
DaysInMonth(with leap years), - an age.
The tutorial Dates and times and the tutorial Calendars and dates showed the date types and the calendar of a month. This lesson is about the rules that business programs add.
The program#
# working days - adding business days, skipping weekends and holidays, week numbers, quarters, month ends and an age
load "dotnet.ring"
using {
"System"
"System.Globalization"
}
# the holidays of this example (fixed dates written as year-month-day)
aHolidays = [ "2030-01-01", "2030-12-25", "2030-12-26" ]
func main
# 1. The day of the week. DayOfWeek is a text: "Monday" ... "Sunday". Many countries have another weekend (Friday and Saturday), so the
# weekend is a list in one place.
oDate = makeDate(2030,3,9)
? "1. " + oDate.Format("yyyy-MM-dd") + " is a " + oDate.DayOfWeek + ", weekend: " + yesNo(isWeekend(oDate))
? " the days of the week of 11-17 March 2030:"
cLine = ""
for i = 11 to 17
cLine += makeDate(2030,3,i).Format("ddd d") + " "
next
? " " + cLine
# 2. Adding days is simple (AddDays), and adding WORKING days is a loop that skips the weekend and the holidays
oStart = makeDate(2030,3,7) # a Thursday
? nl + "2. from Thursday " + oStart.Format("yyyy-MM-dd") + ":"
? " + 1 calendar day: " + oStart.AddDays(1).Format("ddd yyyy-MM-dd")
for nDays in [ 1, 2, 5, 10 ]
? " + " + padRight("" + nDays,2) + " working days: " + addWorkdays(oStart,nDays).Format("ddd yyyy-MM-dd")
next
# 3. Holidays move the result. 2030-12-24 is a Tuesday; the 25th and 26th are holidays, and the 28th and 29th the weekend.
oEve = makeDate(2030,12,24)
? nl + "3. holidays: 3 working days after " + oEve.Format("yyyy-MM-dd") + " is " + addWorkdays(oEve,3).Format("ddd yyyy-MM-dd") + " (the 25th, 26th, 28th and 29th do not count)"
# 4. Counting the working days between two dates (the first day is included, the last is not)
? nl + "4. working days from 2030-03-01 to 2030-04-01: " + countWorkdays(makeDate(2030,3,1),makeDate(2030,4,1))
? " working days in the year 2030: " + countWorkdays(makeDate(2030,1,1),makeDate(2031,1,1))
# 5. Week numbers. The ISO week starts on Monday, and week 1 is the week with the first Thursday of the year: so the first days of
# January can belong to the last week of the year before, and the last days of December to week 1 of the next year.
? nl + "5. ISO weeks:"
for aItem in [ [2030,1,1], [2030,3,9], [2030,12,30], [2027,1,1] ]
oD = makeDate(aItem[1],aItem[2],aItem[3])
? " " + oD.Format("yyyy-MM-dd ddd") + " week " + isoWeek(oD) + " of " + isoYear(oD)
next
# 6. Quarters and the end of a month. DaysInMonth knows the leap years.
? nl + "6. quarters and month ends:"
for nMonth = 1 to 12 step 4
? " month " + padRight("" + nMonth,3) + "quarter " + (floor((nMonth - 1) / 3) + 1) + ", the month has " + daysInMonth(2030,nMonth) + " days"
next
? " February has " + daysInMonth(2030,2) + " days in 2030 and " + daysInMonth(2032,2) + " in 2032"
? " the last day of February 2032 is " + makeDate(2032,2,daysInMonth(2032,2)).Format("yyyy-MM-dd ddd")
# 7. An age: the years between a birth date and a day, one less when the birthday of this year did not come yet
? nl + "7. ages on 2030-03-09:"
for aItem in [ [2000,3,9], [2000,3,10], [1990,12,31], [2030,3,9] ]
? " born " + padRight(makeDate(aItem[1],aItem[2],aItem[3]).Format("yyyy-MM-dd"),11) + "age " + ageOn(makeDate(aItem[1],aItem[2],aItem[3]),makeDate(2030,3,9))
next
func makeDate nYear,nMonth,nDay
return new DateTime(nYear,nMonth,nDay)
# the weekend in one place (Saturday and Sunday here; use ["Friday","Saturday"] for the countries with that weekend)
func isWeekend oDate
cDay = "" + oDate.DayOfWeek
return cDay = "Saturday" or cDay = "Sunday"
func isHoliday oDate
return find(aHolidays,oDate.Format("yyyy-MM-dd")) > 0
func isWorkday oDate
return not isWeekend(oDate) and not isHoliday(oDate)
# n working days after a date (the date itself does not count)
func addWorkdays oDate,nDays
oCurrent = oDate
nLeft = nDays
while nLeft > 0
oCurrent = oCurrent.AddDays(1)
if isWorkday(oCurrent)
nLeft--
ok
end
return oCurrent
func countWorkdays oFrom,oTo
nCount = 0
oCurrent = oFrom
while oCurrent.CompareTo(oTo) < 0
if isWorkday(oCurrent)
nCount++
ok
oCurrent = oCurrent.AddDays(1)
end
return nCount
func isoWeek oDate
return new ISOWeek().GetWeekOfYear(oDate)
func isoYear oDate
return new ISOWeek().GetYear(oDate)
func daysInMonth nYear,nMonth
return new DateTime(nYear,nMonth,1).DaysInMonth(nYear,nMonth)
# the years between two dates, minus one when the birthday of that year has not come yet
func ageOn oBirth,oDay
nAge = oDay.Year - oBirth.Year
if oDay.Month < oBirth.Month or (oDay.Month = oBirth.Month and oDay.Day < oBirth.Day)
nAge--
ok
return nAge
func yesNo lValue
if lValue
return "yes"
ok
return "no"
func padRight cText,nWidth
return cText + copy(" ",nWidth - len(cText))1. 2030-03-09 is a Saturday, weekend: yes
the days of the week of 11-17 March 2030:
Mon 11 Tue 12 Wed 13 Thu 14 Fri 15 Sat 16 Sun 17
2. from Thursday 2030-03-07:
+ 1 calendar day: Fri 2030-03-08
+ 1 working days: Fri 2030-03-08
+ 2 working days: Mon 2030-03-11
+ 5 working days: Thu 2030-03-14
+ 10 working days: Thu 2030-03-21
3. holidays: 3 working days after 2030-12-24 is Tue 2030-12-31 (the 25th, 26th, 28th and 29th do not count)
4. working days from 2030-03-01 to 2030-04-01: 21
working days in the year 2030: 258
5. ISO weeks:
2030-01-01 Tue week 1 of 2030
2030-03-09 Sat week 10 of 2030
2030-12-30 Mon week 1 of 2031
2027-01-01 Fri week 53 of 2026
6. quarters and month ends:
month 1 quarter 1, the month has 31 days
month 5 quarter 2, the month has 31 days
month 9 quarter 3, the month has 30 days
February has 28 days in 2030 and 29 in 2032
the last day of February 2032 is 2032-02-29 Sun
7. ages on 2030-03-09:
born 2000-03-09 age 30
born 2000-03-10 age 29
born 1990-12-31 age 39
born 2030-03-09 age 0How it works#
The weekend and the holidays#
DayOfWeek gives the name of the day as a text. The function isWeekend compares it with "Saturday" and "Sunday"; some countries have a weekend on Friday and Saturday, and then only this function changes. The holidays are a Ring list of dates written as year-month-day, and isHoliday looks for the date in it. Real programs keep the holidays in a file or a table for each country and year (and some holidays, like Easter, move: they come from a rule, not from a fixed date).
Adding working days#
There is no formula that handles weekends and holidays, so addWorkdays does what a person does: it steps one day at a time and counts only the days that are working days. From a Thursday, 2 working days is the next Monday. The date itself does not count. Around 25 December 2030 (a Wednesday) the result jumps over the holidays and the weekend: 3 working days after the 24th is the 31st.
Counting between two dates#
countWorkdays walks from the first date up to, but not including, the second one, and counts the working days: that is a half-open range, the same rule as in most loops. The year 2030 has 261 weekdays and 3 holidays on weekdays, so 258 working days. (Walking day by day is fast enough for a few years; for a very long range count whole weeks first: each week has 5 working days.)
ISO weeks#
The ISO week starts on Monday, and week 1 is the week with the first Thursday of the year. So 1 January can belong to the last week of the year before (1 January 2027 is in week 53 of 2026) and the last days of December can belong to week 1 of the next year (30 December 2030 is in week 1 of 2031). That is why the function gives the week and the year of the week: ISOWeek.GetWeekOfYear and ISOWeek.GetYear. A report by week uses the pair, not the calendar year.
Quarters and month ends#
The quarter of a month is floor((month - 1) / 3) + 1. DateTime.DaysInMonth(year, month) knows the leap years (February 2032 has 29 days), so the last day of a month is the date with that day number; no program should have a table of month lengths.
An age#
The age is the difference of the years, minus one if the birthday of this year has not come. Someone born on 9 March 2000 is 30 on 9 March 2030, and someone born the next day is still 29. Subtracting the days and dividing by 365 is wrong: the leap days add up.
What you learned#
- the weekend and the holidays as data in one place,
- adding and counting working days,
- the ISO week and its year,
- quarters, month lengths and ages.
Exercises#
- Write
previousWorkday(date)andnextWorkday(date). - Write
lastWorkdayOfMonth(year, month). - Add Easter Monday: compute Easter Sunday with the Gregorian algorithm and add one day.
- Print a calendar for one ISO week: the seven dates and which are working days.