FUNCTION
nthWeekDay
Returns the date of the nth weekday within the specified month and year.
You're viewing an older version of this page (#5525). View the current version.
Interface
#include <codecogs/units/date/nthweekday.h>
using namespace Units::Date;
Calculates the date of the nth occurrence of a particular weekday (Sunday to Saturday) within a specified month and year. Many public holidays and other events (see also summertime) are performed on specific days of the week; very often the first or last occurrence of that day within the month. This function uses weekdays within the Gregorian calendar to return the serial Julian date (number of days since 24 November 4714BC).
For example, US events include:
- Martin Luther King Day, third Monday in January
- President's Day, third Monday in February
- Armed Forces Day, third Saturday in May
- Mother's Day, second Sunday in May
- Memorial Day, last Monday in May
- Father's Day, third Sunday in June
- Labor Day, first Monday in September
- Columbus Day, second Monday in October
- Thanksgiving Day, fourth Thursday in November
UK holidays include:
- May Day Bank Holiday, first Monday in May
- Spring Bank Holiday, last Monday in May
- Late Summer Holiday, last Monday in August
Example 1
To compute the UK holidays:
#include <stdio.h>
#include <codecogs/units/date/nthweekday.h>
#include <codecogs/units/date/dateymd.h>
#include <codecogs/units/date/month_str.h>
using namespace Units::Date;
int main()
{
int d,m,y;
for(int i=2004; i<=2006; i++)
{
printf("\nIn %d:",i);
dateYMD(nthWeekDay(1, Monday, i, May), y,m,d); // First Monday
printf("\n May Day Bank Holiday is on: %2d %s",d,month_str(m));
dateYMD(nthWeekDay(0, Monday, i, May), y,m,d); // last Monday
printf("\n Spring Bank Holiday is on: %2d %s",d,month_str(m));
dateYMD(nthWeekDay(0, Monday, i, August), y,m,d); // last Monday
printf("\n Late Summer Holiday is on: %2d %s",d,month_str(m));
}
return 0;
}Output:
In 2004:
May Day Bank Holiday is on: 3 May
Spring Bank Holiday is on: 31 May
Late Summer Holiday is on: 30 August
In 2005:
May Day Bank Holiday is on: 2 May
Spring Bank Holiday is on: 30 May
Late Summer Holiday is on: 29 August
In 2006:
May Day Bank Holiday is on: 1 May
Spring Bank Holiday is on: 29 May
Late Summer Holiday is on: 28 AugustParameters
- Type=1 : WeekDay contains numbers 1 (Sunday) through 7 (Saturday) - default behaviour of Microsoft Excel.
- Type=2 : WeekDay contains numbers 1 (Monday) through 7 (Sunday).
- Type=3 : WeekDay contains numbers 0 (Monday) through 6 (Sunday).
Interactive Calculator
Computing…
Set a range above first to export a graph.