Calculating the day of the week
Encyclopedia
This article details various mathematical algorithm
s to calculate the day of the week for any particular date in the past or future.
A typical application is to calculate the day of the week on which someone was born or some other special event occurred.
7. Modulo 7 is an operation that calculates the remainder of a number being divided by 7. Thus we can treat 7 as 0, 8 as 1, 9 as 2, 18 as 4 and so on; the interpretation of this being that if we signify Sunday as day 0, then 7 days later (i.e. day 7) is also a Sunday, and day 18 will be the same as day 4, which is a Thursday since this falls 4 days after Sunday.
The basic approach of nearly all of the methods to calculate the day of the week begins by starting from a known pair (such as January 1, 1800 as a Wednesday), determining the number of days between the known day and the day that you are trying to determine, and using arithmetic modulo
7 to find a new numerical weekday.
One standard approach is to look up (or calculate, using a known rule) the first weekday of a given century, and then look up (or calculate) modifiers for the years, the months, and the days of the month. Eventually, you end up with a sum of days that remain, and you use modulo 7 on this number to arrive at the final weekday.
Some methods do all the additions first and then cast out sevens whereas others cast them out at each step. Either way is quite permissible; the former is better when using calculators and in computer programs, the latter for mental calculation (it is quite possible to do all the calculations in one's head with a little practice).
None of the methods given here perform range checks, so that unreasonable dates will produce erroneous results.
In a leap year
, January and February correspond to different months than in a common year
, since February 29 means each subsequent month starts a day later.
Here's how the months correspond:
Note that in the months table below, corresponding months have the same number, a fact which follows directly from the definition.
. For example, 2011 is a common year starting on Saturday
, meaning that 2011 corresponds to the 2005 calendar year
. 2012, on the other hand, is a leap year starting on Sunday
, meaning that the first two months of the year begin as they do 2006 (i.e. January 1 is a Sunday and February 1 is a Wednesday) but because of leap day the last ten months correspond to 2007 (i.e. March 1 is a Thursday, etc.).
s assigns a letter from A through G to each day of the year. In a leap year, February 24, the bissextile day, does not have a distinct letter. This causes all subsequent Sundays to be associated with a different dominical letter than those in the beginning of the year, so all leap years get two dominical letters. In this system, the "dominical letter" for a year is the letter which corresponds to the Sundays of that year.
. Britain and its colonies started using the Gregorian calendar on Thursday, September 14,
1752 (the previous day was Wednesday, September 2, 1752 (Old Style)
). The areas now forming the United States adopted the
calendar at different times depending on the colonial power: Spain and France had been using it since 1582, while Russia was still using the
Julian calendar
when Alaska was purchased from it in 1867.
The method requires one to know four input parameters, in order to find five numbers to sum. Using modulus to restrict results to 0 through
6, the day of the week can be determined. Since this method uses the "zeroeth" day, we can add the day of the month directly (without
subtracting 1). Examples of the evaluation of this method are below. The four inputs and five resulting summands are:
Now let's try September 18, 1783.
Let's try leap year
January 1, 2000
Finally, let's try June 19, 2054
1800–1899 2
1900–1999 0
2000–2099 6
2100–2199 4
2200–2299 2
2300–2399 0
2400–2499 6
2500–2599 4
2600–2699 2
February 3 (in leap year 2)
March 3
April 6
May 1
June 4
July 6
August 2
September 5
October 0
November 3
December 5
Monday 1
Tuesday 2
Wednesday 3
Thursday 4
Friday 5
Saturday 6
One can add constants (modulo 7) to these three tables provided the constant you add to the day table is equal to the sum of the constants you add to the centuries table and the months table modulo 7.
Where
Y: year-1 for January or February,
year for the rest of the year
d: day (1 to 31)
m: shifted month (March=1,...February=12), eg. ((month + 9) % 12) + 1
y: last 2 digits of Y
c: first 2 digits of Y
w: day of week (0=Sunday,..6=Saturday)
function, which is adaptable to other high level programming languages with minor changes (a 6502 assembly language
version of Sakamoto's algorithm exists as well). Devised by Tomohiko Sakamoto in 1993, it is accurate for any date in the range October 15, 1582–December 31, 9999:
int dow(int y, int m, int d)
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
In the above, y, m and d are, respectively the year (e.g., 1988), month (1-12) and day of the month (1-31). The function returns 0 = Sunday, 1 = Monday, etc.
.
the day, month and year are given. It uses a months' table in a manner identical to Sakamoto's method. Babwani's method
differs from Zeller's Algorithm as months are numbered normally from 1 for January to 12 for December.
Algorithm
In mathematics and computer science, an algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Algorithms are used for calculation, data processing, and automated reasoning...
s to calculate the day of the week for any particular date in the past or future.
A typical application is to calculate the day of the week on which someone was born or some other special event occurred.
Introduction
In order to determine weekdays from numerical operations, it is necessary to convert weekdays (Sunday, Monday, etc.) to numbers from 0 to 6 (or 1 to 7). This is done with arithmetic moduloModular arithmetic
In mathematics, modular arithmetic is a system of arithmetic for integers, where numbers "wrap around" after they reach a certain value—the modulus....
7. Modulo 7 is an operation that calculates the remainder of a number being divided by 7. Thus we can treat 7 as 0, 8 as 1, 9 as 2, 18 as 4 and so on; the interpretation of this being that if we signify Sunday as day 0, then 7 days later (i.e. day 7) is also a Sunday, and day 18 will be the same as day 4, which is a Thursday since this falls 4 days after Sunday.
The basic approach of nearly all of the methods to calculate the day of the week begins by starting from a known pair (such as January 1, 1800 as a Wednesday), determining the number of days between the known day and the day that you are trying to determine, and using arithmetic modulo
Modular arithmetic
In mathematics, modular arithmetic is a system of arithmetic for integers, where numbers "wrap around" after they reach a certain value—the modulus....
7 to find a new numerical weekday.
One standard approach is to look up (or calculate, using a known rule) the first weekday of a given century, and then look up (or calculate) modifiers for the years, the months, and the days of the month. Eventually, you end up with a sum of days that remain, and you use modulo 7 on this number to arrive at the final weekday.
Some methods do all the additions first and then cast out sevens whereas others cast them out at each step. Either way is quite permissible; the former is better when using calculators and in computer programs, the latter for mental calculation (it is quite possible to do all the calculations in one's head with a little practice).
None of the methods given here perform range checks, so that unreasonable dates will produce erroneous results.
Corresponding months
"Corresponding months" are those months within the calendar year that start on the same day. For example, September and December correspond, because September 1 falls on the same day as December 1. Months can only correspond if the number of days between their first days is divisible by 7, or in other words, if their first days are a whole number of weeks apart. For example, February corresponds to March because February has 28 days, a number divisible by 7, 28 days being exactly four weeks.In a leap year
Leap year
A leap year is a year containing one extra day in order to keep the calendar year synchronized with the astronomical or seasonal year...
, January and February correspond to different months than in a common year
Common year
A common year is a common type of calendar year. It has exactly 365 days and so is not a leap year. More generally, it is a calendar year without intercalation....
, since February 29 means each subsequent month starts a day later.
Here's how the months correspond:
- Common year
- January and October.
- February, March and November.
- April and July.
- No month corresponds to August.
- Leap year
- January, April and July.
- February and August.
- March and November.
- No month corresponds to October.
- All Years
- September and December.
- No month corresponds to May or June.
Note that in the months table below, corresponding months have the same number, a fact which follows directly from the definition.
Corresponding years
There are seven possible days that a year can start on, and leap years will alter the day of the week after February 29. This means that there are 14 configurations that a year can have. All the configurations can be referenced by a Dominical letterDominical letter
Dominical letters are letters A, B, C, D, E, F and G assigned to days in a cycle of seven with the letter A always set against 1 January as an aid for finding the day of the week of a given calendar date and in calculating Easter....
. For example, 2011 is a common year starting on Saturday
Common year starting on Saturday
This is the calendar for any common year starting on Saturday, January 1 . Examples: Gregorian years 1994, 2005, 2011 and 2022...
, meaning that 2011 corresponds to the 2005 calendar year
Calendar year
Generally speaking, a calendar year begins on the New Year's Day of the given calendar system and ends on the day before the following New Year's Day. By convention, a calendar year consists of a natural number of days. To reconcile the calendar year with an astronomical cycle , certain years...
. 2012, on the other hand, is a leap year starting on Sunday
Leap year starting on Sunday
This is the calendar for any leap year starting on Sunday, January 1 , such as 1956, 1984, 2012, 2040, or 2068.This is the only leap year with three occurrences of Friday the 13th, each three months apart in January, April, and July....
, meaning that the first two months of the year begin as they do 2006 (i.e. January 1 is a Sunday and February 1 is a Wednesday) but because of leap day the last ten months correspond to 2007 (i.e. March 1 is a Thursday, etc.).
Dominical letters
The system of dominical letterDominical letter
Dominical letters are letters A, B, C, D, E, F and G assigned to days in a cycle of seven with the letter A always set against 1 January as an aid for finding the day of the week of a given calendar date and in calculating Easter....
s assigns a letter from A through G to each day of the year. In a leap year, February 24, the bissextile day, does not have a distinct letter. This causes all subsequent Sundays to be associated with a different dominical letter than those in the beginning of the year, so all leap years get two dominical letters. In this system, the "dominical letter" for a year is the letter which corresponds to the Sundays of that year.
A tabular method to calculate the day of the week
This method is valid for the Gregorian calendarGregorian calendar
The Gregorian calendar, also known as the Western calendar, or Christian calendar, is the internationally accepted civil calendar. It was introduced by Pope Gregory XIII, after whom the calendar was named, by a decree signed on 24 February 1582, a papal bull known by its opening words Inter...
. Britain and its colonies started using the Gregorian calendar on Thursday, September 14,
1752 (the previous day was Wednesday, September 2, 1752 (Old Style)
Old Style and New Style dates
Old Style and New Style are used in English language historical studies either to indicate that the start of the Julian year has been adjusted to start on 1 January even though documents written at the time use a different start of year ; or to indicate that a date conforms to the Julian...
). The areas now forming the United States adopted the
calendar at different times depending on the colonial power: Spain and France had been using it since 1582, while Russia was still using the
Julian calendar
Julian calendar
The Julian calendar began in 45 BC as a reform of the Roman calendar by Julius Caesar. It was chosen after consultation with the astronomer Sosigenes of Alexandria and was probably designed to approximate the tropical year .The Julian calendar has a regular year of 365 days divided into 12 months...
when Alaska was purchased from it in 1867.
The method requires one to know four input parameters, in order to find five numbers to sum. Using modulus to restrict results to 0 through
6, the day of the week can be determined. Since this method uses the "zeroeth" day, we can add the day of the month directly (without
subtracting 1). Examples of the evaluation of this method are below. The four inputs and five resulting summands are:
- Century: First, we can either refer to the centuries table below or use the rule: Where is the first two digits of the year, define . With Sunday being day 0, these numbers are the day of the week that January 0 (December 31 of the previous year), fell on year 0 of the century, with one added to the value when century mod 4 is 0. This corrects for the fact that the next step undercounts the number of leap days by 1 on centuries whose first two digits are evenly divisible by 4, such as the 21st century, where year 0 (2000) was a leap year. (See Leap yearLeap yearA leap year is a year containing one extra day in order to keep the calendar year synchronized with the astronomical or seasonal year...
). - Year: Because there are 365 days in a common year, which is 52 weeks plus 1 day, each year will start on the day of the week after that starting the preceding year. Each leap year has of course one more day than a common year. Assuming we know on which day a century starts (from above), if we add the number of years elapsed since the start of the century, plus the number of leap years that have elapsed since the start of the century, we get the day of the week on which the year starts. Where is the last two digits of the year, define
- Month: We refer to the months table below to work out on which day of the week a month starts. Notice that January starts on day 0, which is simply another way of saying that the year and January of that year start on the same day. The months table shown allows for leap years; other methods leave the correction to the end and then deduct 1 from the final figure if the month is a January or February of a leap year.
- Day of the Month: Once we know on which day of the week the month starts, we simply add the day of the month to find the final result (noting that as mentioned above, we've been working with the "zeroeth" day of the month as the start).
Examples
Now for an example of the complete method, let's use April 24, 1982.- Look up the 1900s in the centuries table: 0
- Note the last two digits of the year: 82
- Divide the 82 by 4: 82/4 = 20.5 and drop the fractional part: 20
- Look up April in the months table: 6
- Add all numbers from steps 1–4 to the day of the month (in this case, 24): 0+82+20+6+24=132.
- Divide the sum from step 5 by 7 and find the remainder: 132/7=18 remainder 6
- Find the remainder in the days table: 6=Saturday.
Now let's try September 18, 1783.
- Look up the 1700s in the centuries table: 4
- Note the last two digits of the year: 83
- Divide the 83 by 4: 83/4 = 20.75 and drop the fractional part: 20
- Look up September in the months table: 5
- Add all numbers from steps 1–4 to the day of the month (in this case, 18): 4+83+20+5+18=130.
- Divide the sum from step 5 by 7 and find the remainder: 130/7=18 remainder 4
- Find the remainder in the days table: 4=Thursday.
Let's try leap year
Leap year
A leap year is a year containing one extra day in order to keep the calendar year synchronized with the astronomical or seasonal year...
January 1, 2000
- Look up the 2000s in the centuries table: 6
- Note the last two digits of the year: 00
- Divide the 00 by 4: 0/4 = 0 and drop the fractional part: 0
- Look up January in the months table: 6 (leap)
- Add all numbers from steps 1–4 to the day of the month (in this case, 1): 6+00+0+6+1=13.
- Divide the sum from step 5 by 7 and find the remainder: 13/7=1 remainder 6
- Find the remainder in the days table: 6=Saturday.
Finally, let's try June 19, 2054
- Look up the 2000s in the centuries table: 6
- Note the last two digits of the year: 54
- Divide the 54 by 4: 54/4 = 13.5 and drop the fractional part: 13
- Look up June in the months table: 4
- Add all numbers from steps 1–4 to the day of the month (in this case, 19): 6+54+13+4+19=96.
- Divide the sum from step 5 by 7 and find the remainder: 96/7=13 remainder 5
- Find the remainder in the days table: 5=Friday.
Centuries table
1700–1799 4 (Still the Old-Style Calendar in British Territories until 1752)1800–1899 2
1900–1999 0
2000–2099 6
2100–2199 4
2200–2299 2
2300–2399 0
2400–2499 6
2500–2599 4
2600–2699 2
Months table
January 0 (in leap year 6)February 3 (in leap year 2)
March 3
April 6
May 1
June 4
July 6
August 2
September 5
October 0
November 3
December 5
Days table
Sunday 0Monday 1
Tuesday 2
Wednesday 3
Thursday 4
Friday 5
Saturday 6
One can add constants (modulo 7) to these three tables provided the constant you add to the day table is equal to the sum of the constants you add to the centuries table and the months table modulo 7.
Gaussian algorithm
The Gaussian algorithm is much more compact and works without any lookup tables. A slight disadvantage is the unusual month and year counting convention. The formula isWhere
Y: year-1 for January or February,
year for the rest of the year
d: day (1 to 31)
m: shifted month (March=1,...February=12), eg. ((month + 9) % 12) + 1
y: last 2 digits of Y
c: first 2 digits of Y
w: day of week (0=Sunday,..6=Saturday)
Zeller’s algorithm
In Zeller’s algorithm, the months are numbered from 3 for March to 14 for February. The year is assumed to begin in March; this means, for example, that January 1995 is to be treated as month 13 of 1994.Sakamoto's Method
A tabular alternative to Mike Keith's method is embodied in the following ANSI CANSI C
ANSI C refers to the family of successive standards published by the American National Standards Institute for the C programming language. Software developers writing in C are encouraged to conform to the standards, as doing so aids portability between compilers.-History and outlook:The first...
function, which is adaptable to other high level programming languages with minor changes (a 6502 assembly language
Assembly language
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...
version of Sakamoto's algorithm exists as well). Devised by Tomohiko Sakamoto in 1993, it is accurate for any date in the range October 15, 1582–December 31, 9999:
int dow(int y, int m, int d)
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
In the above, y, m and d are, respectively the year (e.g., 1988), month (1-12) and day of the month (1-31). The function returns 0 = Sunday, 1 = Monday, etc.
.
Babwani's Method
Sohael Babwani developed an alternative method that enables one not only to find the day of the week but also a date, whenthe day, month and year are given. It uses a months' table in a manner identical to Sakamoto's method. Babwani's method
differs from Zeller's Algorithm as months are numbered normally from 1 for January to 12 for December.
See also
- ISO 8601ISO 8601ISO 8601 Data elements and interchange formats – Information interchange – Representation of dates and times is an international standard covering the exchange of date and time-related data. It was issued by the International Organization for Standardization and was first published in 1988...
- Doomsday rule
- Perpetual calendarPerpetual calendarA perpetual calendar is a calendar which is good for a span of many years, such as the Runic calendar.- General information :...
- Julian day#Calculation
- Perpetual Calendar of 800 Years
External links
- Encyclopedia of mental Weekday Calculation by Hans-Christian Solka, 2010
- Compact tabular method for memorization, also for the Julian calendar
- An algorithm for both Gregorian and Julian calendars
- When countries changed from the Julian calendar
- World records for mentally calculating the day of the week in the Gregorian calendar