The determination of the day of the week for any date may be performed with a variety of algorithms. In addition, perpetual calendars require no calculation by the user, and are essentially lookup tables.
A typical application is to calculate the day of the week on which someone was born or a specific event occurred.
Concepts
In numerical calculation, the days of the week are represented as weekday numbers. If Monday is the first day of the week, the days may be coded 1 to 7, for Monday through Sunday, as is practiced in ISO 8601. The day designated with 7 may also be counted as 0, by applying the arithmetic modulo 7, which calculates the remainder of a number after division by 7. Thus, the number 7 is treated as 0, the number 8 as 1, the number 9 as 2, the number 18 as 4, and so on. If Sunday is counted as day 1, then 7 days later (i.e.day 8) is also a Sunday, and day 18 is the same as day 4, which is a Wednesday since this falls three days after Sunday (i.e.).
{| class="wikitable"
|-
! Standard
! Monday
! Tuesday
! Wednesday
! Thursday
! Friday
! Saturday
! Sunday
! Usage examples
|-
| ISO 8601
| 1
| 2
| 3
| 4
| 5
| 6
| 7
| (4DOS); (HP Prime)
|-
|
| 0
| 1
| 2
| 3
| 4
| 5
| 6
|
|-
|
| 2
| 3
| 4
| 5
| 6
| 7
| 1
| (NetWare, DR-DOS); (4DOS)
Some methods do all the additions first and then cast out sevens, whereas others cast them out at each step, as in Lewis Carroll's method. Either way can have practical use: the former is easier for calculators and computer programs, whereas the latter is easier for mental calculation. None of the methods given here perform range checks, so unreasonable dates will produce erroneous results.
Corresponding days
Every seventh day in a month has the same name as the previous:
{| class="wikitable"
|-
!Day of<br />the month ||
|-
|00 07 14 21 28 ||0
|-
|01 08 15 22 29 ||1
|-
|02 09 16 23 30 ||2
|-
|03 10 17 24 31 ||3
|-
|04 11 18 25 ||4
|-
|05 12 19 26 ||5
|-
|06 13 20 27 ||6
|}
Corresponding months
"Corresponding months" are those months within the calendar year that start on the same day of the week. For example, September and December correspond, because 1 September falls on the same day as 1 December (as there are precisely thirteen 7-day weeks between the two dates). 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 of a common year corresponds to March because February has 28 days, a number divisible by 7, 28 days being exactly four weeks.
In a leap year, January and February correspond to different months than in a common year, since adding 29 February means each subsequent month starts a day later.
January corresponds to October in common years and April and July in leap years. February corresponds to March and November in common years and August in leap years. March always corresponds to November, April always corresponds to July, and September always corresponds to December. August does not correspond to any other month in a common year. October doesn't correspond to any other month in a leap year. May and June never correspond to any other month.
In the months table below, corresponding months have the same number, a fact which follows directly from the definition.
{| class="wikitable" style="text-align:center;"
|-
!Common years!!Leap years!!All years!!
|-
| Jan || || Oct || 0
|-
| || || May || 1
|-
| || Feb || Aug || 2
|-
| Feb || || Mar Nov || 3
|-
| || || Jun || 4
|-
| || || Sept Dec || 5
|-
| || Jan || Apr July || 6
|}
Corresponding years
<!--The example given is to be updated every 4 years. Its next update should be in 2029, then 2033, and so on.-->
There are seven possible days that a year can start on, and leap years will alter the day of the week after 29 February. This means that there are 14 configurations that a year can have. All the configurations can be referenced by a dominical letter, but as 29 February has no letter allocated to it, a leap year has two dominical letters, one for January and February and the other (one step back in the alphabetical sequence) for March to December.
2025 is a common year starting on Wednesday, which means that it corresponds to the 2014 calendar year. The first two months of 2025 correspond to the first two months of 2020. 2026 is a common year starting on Thursday, which means that it corresponds to the 2015 calendar year. The last ten months of 2026 correspond to the last ten months of 2020. 2027 is a common year starting on Friday, which means that it corresponds to the 2021 calendar year. 2028 is a leap year starting on Saturday, which means that it corresponds to the 2000 calendar year. The first two months of 2028 correspond to the first two months of 2022. The last ten months of 2028 correspond to the last ten months of 2023.
Each leap year repeats once every 28 years, and every common year repeats once every 6 years and twice every 11 years. For instance, the last occurrence of a leap year starting on Monday was 2024 and the next occurrence will be 2052. Likewise, the next common years starting on Wednesday will be 2031, 2042, and then 2053. Both of these statements are true unless a leap year is skipped, which will not happen until 2100.
For details, see the table below:
{| class="wikitable" style="text-align:center"
|-
! Year of the<br />century mod 28 ||
|-
| <span style="color:blue;">00</span> 06 <span style="color:red;">12</span> 17 23 ||0
|-
| 01 07 <span style="color:blue;">12</span> 18 <span style="color:red;">24</span> ||1
|-
| 02 <span style="color:red;">08</span> 13 19 <span style="color:blue;">24</span> ||2
|-
| 03 <span style="color:blue;">08</span> 14 <span style="color:red;">20</span> 25 ||3
|-
| <span style="color:red;">04</span> 09 15 <span style="color:blue;">20</span> 26 ||4
|-
| <span style="color:blue;">04</span> 10 <span style="color:red;">16</span> 21 27 ||5
|-
| 05 11 <span style="color:blue;">16</span> 22 <span style="color:red;">00</span> ||6
|}
Notes:
- Black means all the months of a common year
- <span style="color:red;">Red</span> means the first two months of a leap year
- <span style="color:blue;">Blue</span> means the last ten months of a leap year
Corresponding centuries
{| class="wikitable" style="text-align:center"
|-
!Julian century<br />mod 700||Gregorian century<br />mod 400||Day
|-
|400: 1100 1800 ...||300: 1500 1900 ...||Sun
|-
|300: 1000 1700 ...||||Mon
|-
|200 0900 1600 ...||200: 1800 2200 ...||Tue
|-
|100 0800 1500 ...||||Wed
|-
|700: 1400 2100 ...||100: 1700 2100 ...||Thu
|-
|600: 1300 2000 ...||||Fri
|-
|500: 1200 1900 ...||000: 1600 2000 ...||Sat
|}
"Year 000" is, in normal chronology, the year 1 BC (which precedes AD 1). In astronomical year numbering the year 0 comes between 1 BC and AD 1. In the proleptic Julian calendar, (that is, the Julian calendar as it would have been if it had been operated correctly from the start), 1 BC starts on Thursday. In the proleptic Gregorian calendar, (called such because it wasn't devised until 1582), 1 BC starts on Saturday.
Tabular methods to calculate the day of the week
Complete table: Julian and Gregorian calendars
For Julian dates before 1300 and after 1999 the year in the table which differs by an exact multiple of 700 years should be used. For Gregorian dates after 2299, the year in the table which differs by an exact multiple of 400 years should be used. The values "" through "" indicate the remainder when the hundreds value is divided by 7 and 4 respectively, indicating how the series extend in either direction. Both Julian and Gregorian values are shown 1500–1999 for convenience. Bold figures (e.g., 04) denote leap year. If a year ends in 00 and its hundreds are in bold it is a leap year. Thus 19 indicates that 1900 is not a Gregorian leap year, (but 19 in the Julian column indicates that it is a Julian leap year, as are all Julian x00 years). 20 indicates that 2000 is a leap year. Use Jan and Feb only in leap years.
{| class=wikitable
!colspan=2 style="line-height:10px; border-bottom:none"|<small>Hundreds of years</small>||colspan=4 rowspan=2|Remaining year digits||colspan=5 rowspan=2|Month
!colspan=1 rowspan=2 ||colspan=1 rowspan=2|#
|-
! style="border-top:none; line-height:10px"|<small>Julian<br />(r ÷ 7)</small>
! style="border-top:none; line-height:10px"|<small>Gregorian<br />(r ÷ 4)</small>
|-
|<small>r5</small> 19||16 20 <small>r0</small>||00 06 17 23||28 34 45 51||56 62 73 79||84 90 ||Jan|| || || || Oct||Sa||0
|-
|<small>r4</small> 18||15 19 <small>r3</small>||01 07 12 18 ||29 35 40 46||57 63 68 74 ||85 91 96|| || || May|| || ||Su||1
|-
|<small>r3</small> 17||||02 13 19 24||30 41 47 52||58 69 75 80||86 97|| Feb|| || || Aug|| ||M||2
|-
|<small>r2</small> 16||18 22 <small>r2</small>||03 08 14 25||31 36 42 53||59 64 70 81||87 92 98||Feb|| Mar|| || || Nov||Tu||3
|-
|<small>r1</small> 15|||| 09 15 20 26|| 37 43 48 54|| 65 71 76 82|| 93 99 || || || Jun|| || ||W||4
|-
|<small>r0</small> 14||17 21 <small>r1</small>||04 10 21 27||32 38 49 55||60 66 77 83||88 94 || || || ||Sep ||Dec||Th||5
|-
|<small>r6</small> 13||||05 11 16 22||33 39 44 50||61 67 72 78||89 95 ||Jan|| Apr|| Jul|| || ||F||6
|}
For determination of the day of the week (1 January 2000, Saturday)
- <span style="background-color:#FFFF88">the day of the month: 1 ~ 31 (1)</span>
- <span style="background-color:#FFFF88">the month: (6)</span>
- <span style="background-color:#FF8888">the year: (0)</span>
- <span style="background-color:#88FF88">the century mod 4 for the Gregorian calendar and mod 7 for the Julian calendar (0)</span>.
- <span style="background-color:#BBBBFF">adding 1+6+0+0=7. Dividing by 7 leaves a remainder of 0, so the day of the week is Saturday.</span>
The formula is w = (d + m + y + c) mod 7.
Revised Julian calendar
Note that the date (and hence the day of the week) in the Revised Julian and Gregorian calendars is the same from 14 October 1923 to 28 February AD 2800 inclusive and that for large years it may be possible to subtract 6300 or a multiple thereof before starting so as to reach a year which is within or closer to the table.
To look up the weekday of any date for any year using the table, subtract 100 from the year, divide the difference by 100, multiply the resulting quotient (omitting fractions) by seven and divide the product by nine. Note the quotient (omitting fractions). Enter the table with the Julian year, and just before the final division add 50 and subtract the quotient noted above.
The following is an example of calculating the day of the week for 27 January 8315:
= 2015, = 1915, = 19 remainder 15, = 133, = 14 remainder }. 2015 is 700 years ahead of 1315, so 1315 is used. From table: for hundreds (13): 6. For remaining digits (15): 4. For month (January): 0. For date (27): 27. . . Thus, the day of the week is Thursday.
Dominical letter
To find the dominical letter, calculate the day of the week for either 1 January or 1 October, and assign a letter with Sunday corresponding to A, Saturday corresponding to B, ... and Monday corresponding to G.
Leap years have two Sunday letters, so for January and February calculate the day of the week for 1 January; and for March to December calculate the day of the week for 1 October.
Leap years are all years which divide exactly by four with the following exceptions:
In the Gregorian calendar – all years which divide exactly by 100 (other than those which divide exactly by 400).
In the Revised Julian calendar – all years which divide exactly by 100 (other than those which give remainder 200 or 600 when divided by 900).
The Doomsday
The Doomsday algorithm invented by the mathematician John Horton Conway computes the weekday of any date in any year in the Gregorian calendar (or, with slight modification, in the Julian calendar). It can be performed mentally as the memorization and calculation required is limited.
Result
The following is a table for finding the day of the week without calculation.
{|class="wikitable" style="text-align:center"
|-
!colspan=5|Index || Mon<br />A || Tue<br />B || Wed<br />C || Thu<br />D || Fri<br />E || Sat<br />F || Sun<br />G !!colspan=18, rowspan=2|Perpetual Gregorian and Julian calendar<br />Use <span style="color:red;">Jan</span> and <span style="color:red;">Feb</span> for <span style="color:red;">leap years</span><br />Date letter in year row for the letter in century row
All the C days are doomsdays
|-
!Julian<br />century!!Gregorian<br />century!!colspan=3|Date
| 01<br />08<br />15<br />22<br />29 || 02<br />09<br />16<br />23<br />30 || 03<br />10<br />17<br />24<br />31 || 04<br />11<br />18<br />25<br /><br />|| 05<br />12<br />19<br />26<br /><br />|| 06<br />13<br />20<br />27<br /><br />|| 07<br />14<br />21<br />28<br /><br />
|-
| <span style="color:red;">12 19</span> || <span style="color:red;">16 20</span> || Apr || Jul || <span style="color:red;">Jan</span> || <span style="color:blue;">G</span> || A || B || C || D || E || F ||01 || 07 ||<span style="color:red;">12</span>|| 18 || || 29 || 35 ||<span style="color:red;">40</span>|| 46 || || 57 || 63 ||<span style="color:red;">68</span>|| 74 || || 85 || 91 ||<span style="color:red;">96</span>
|-
| <span style="color:red;">13 20</span> || || Sep || Dec || ||<span style="color:blue;">F</span> || G || A || B || C || D || E || 02 || || 13 || 19 ||<span style="color:red;">24</span>|| 30 || || 41 || 47 ||<span style="color:red;">52</span>|| 58 || || 69 ||75 ||<span style="color:red;">80</span>||86 || || 97
|-
| <span style="color:red;">14 21</span> || 17 21 || |Jun || || || <span style="color:blue;">E</span> || F || G || A || B || C || D || 03 ||<span style="color:red;">08</span>|| 14 || || 25 || 31 ||<span style="color:red;">36</span>|| 42 || || 53 || 59 ||<span style="color:red;">64</span>|| 70 || || 81 || 87 ||<span style="color:red;">92</span>|| 98
|-
| <span style="color:red;">15 22</span> || || Feb || Mar || Nov || <span style="color:blue;">D</span> || E || F || | G || A || B || C || || 09 || 15 ||<span style="color:red;">20</span>|| 26 || || 37 || 43 ||<span style="color:red;">48</span>|| 54 || || 65 || 71 ||<span style="color:red;">76</span>|| 82 || || 93 || 99
|-
| <span style="color:red;">16 23</span> || 18 22 || Aug || <span style="color:red;">Feb</span> || || <span style="color:blue;">C</span> || D || E || F || G || A || B ||<span style="color:red;">04</span>|| 10 || || 21 || 27||<span style="color:red;">32</span>|| 38 || || 49 || 55 ||<span style="color:red;">60</span>|| 66 || || 77 || 83 ||<span style="color:red;">88</span>|| 94 ||
|-
| <span style="color:red;">17 24</span> || || May || || || <span style="color:blue;">B</span> || C || D || E || F || G || A || 05 || 11 ||<span style="color:red;">16</span>|| 22 || || 33 || 39 ||<span style="color:red;">44</span>|| 50 || || 61 || 67 ||<span style="color:red;">72</span>|| 78 || || 89 || 95 ||
|-
| <span style="color:red;">18 25</span> || 19 23 || Jan || Oct || || <span style="color:blue;">A</span> || <span style="color:blue;">B</span> || <span style="color:blue;">C</span> || <span style="color:blue;">D</span> || <span style="color:blue;">E</span> || <span style="color:blue;">F</span> || <span style="color:blue;">G</span> || 06 || || 17 || 23 ||<span style="color:red;">28</span>|| 34 || || 45 || 51 ||<span style="color:red;">56</span>|| 62 || || 73 || 79 ||<span style="color:red;">84</span>|| 90 || ||<span style="color:red;">0</span>0
|-
!colspan=2, rowspan=2|[Year/100] || colspan=3|Gregorian<br />century
| <span style="color:red;">20<br />16</span> || || 21<br />17 || || 22<br />18|| || 23<br />19
!colspan=18, rowspan=2|Year mod 100
|-
!colspan=3|Julian<br />century
| <span style="color:red;">19<br />12</span> || <span style="color:red;">20<br />13</span> || <span style="color:red;">21<br />14</span> || <span style="color:red;">22<br />15</span> || <span style="color:red;">23<br />16</span> || <span style="color:red;">24<br />17</span> || <span style="color:red;">25<br />18</span>
|}
Examples:
- For common method
:26 December 1893 (GD)
December is in row <span style="color:blue;">F</span> and 26 is in column <span style="color:blue;">E</span>, so the letter for the date is C located in row <span style="color:blue;">F</span> and column <span style="color:blue;">E</span>. 93 (year mod 100) is in row <span style="color:blue;">D</span> (year row) and the letter C in the year row is located in column <span style="color:blue;">G</span>. 18 ([year/100] in the Gregorian century column) is in row <span style="color:blue;">C</span> (century row) and the letter in the century row and column <span style="color:blue;">G</span> is B, so the day of the week is Tuesday.
:13 October 1307 (JD)
October 13 is a F day. The letter F in the year row (07) is located in column <span style="color:blue;">G</span>. The letter in the century row (13) and column <span style="color:blue;">G</span> is E, so the day of the week is Friday.
:1 January 2000 (GD)
January 1 corresponds to G, G in the year row (<span style="color:red;">0</span>0) corresponds to F in the century row (<span style="color:red;">20</span>), and F corresponds to Saturday.
A pithy formula for the method: "Date letter (G), letter (G) is in year row (<span style="color:red;">0</span>0) for the letter (F) in century row (<span style="color:red;">20</span>), and for the day, the letter (F) become weekday (Saturday)".
The Sunday letter method
Each day of the year (other than 29 February) has a letter allocated to it in the recurring sequence ABCDEFG. The series begins with A on 1 January and continues to A again on 31 December. The Sunday letter is the one which stands against all the Sundays in the year. Since 29 February has no letter, this means that the Sunday letter for March to December is one step back in the sequence compared to that for January and February. The letter for any date will be found where the row containing the month (in black) at the left of the "Latin square" meets the column containing the date above the "Latin square". The Sunday letter will be found where the column containing the century (below the "Latin square") meets the row containing the year's last two digits to the right of the "Latin square". For a leap year, the Sunday letter thus found is the one which applies to March to December.
So, for example, to find the weekday of 16 June 2020:
Column "20" meets row "20" at "D". Row "June" meets column "16" at "F". As F is two letters on from D, so the weekday is two days on from Sunday, i.e. Tuesday.
Mathematical algorithms
Rata Die
The Rata Die method works by adding up the number of days that has passed since a date of known day of the week . The day of-the-week is then given by , conforming to whatever convention was used to encode .
For example, the date of 13 August 2009 is 733632 days from 1 January AD 1. Taking the number mod 7 yields 4, hence a Thursday.
Gauss's algorithm
Carl Friedrich Gauss described a method for calculating the day of the week for 1 January in any given year in a handwritten note in a collection of astronomical tables. He never published it. It was finally included in his collected works in 1927. Compared to Rata Die, the result helps simplify the counting of years.
Gauss's method was applicable to the Gregorian calendar. He numbered the weekdays from 0 to 6 starting with Sunday. He defined the following operation.
; Inputs
: Year number , month number , date number .
:
; Output
: Day of year.
; Procedure
:# First determine the day-of-week of 1 January.
:#* For a Gregorian calendar, the weekday is
Disparate variation
The following formula is an example of a version without a lookup table. The year is assumed to begin in March, meaning dates in January and February should be treated as being part of the preceding year. The formula for the Gregorian calendar is
: <math>w = \left(d + \lfloor 2.6m - 0.2 \rfloor + y + \left\lfloor\frac{y}{4}\right\rfloor + \left\lfloor\frac{c}{4}\right\rfloor - 2c\right) \bmod 7,</math>
where
- is the day of the month (1 to 31)
- is the shifted month (March = 1,...,February = 12)
- is the year unless is 11 = January or 12 = February which are considered part of the preceding year, giving
- is the century given by <math>c = \lfloor Y/100 \rfloor</math>
- is the year relative to the century, given by <math>y = Y - 100c</math>, or simply the last 2 digits of
- is the day of the week (0 = Sunday,...,6 = Saturday)
{| class="wikitable" style="text-align:center;"
|+Table of month offsets (<math>\lfloor 2.6m - 0.2 \rfloor</math>)
! Months
| Mar || Apr || May || Jun || Jul || Aug || Sep || Oct || Nov || Dec || Jan || Feb
|-
! Offset
| 2 || 5 || 7 || 10 || 12 || 15 || 18 || 20 || 23 || 25 || 28 || 31
|}
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.
The formula for the Gregorian calendar is<br />
<math display="block">w \equiv \left(\left\lfloor \frac{13(m+1)}{5}\right\rfloor+\left\lfloor \frac{y}{4}\right\rfloor+\left\lfloor\frac{c}{4}\right\rfloor + d+y-2c\right) \bmod 7 </math>
where
- <math>d</math> is the day of the month (1 to 31)
- <math>m</math> is the shifted month (March=3,...January = 13, February=14)
- <math>Y</math> is the year unless <math>m</math> is 13 = January or 14 = February which are considered part of the preceding year giving <math> Y = \textit{year} -1</math>
- <math>c</math> is the century given by <math>c = \lfloor Y/100 \rfloor</math>
- <math>y</math> is the year relative to the century, given by <math>y = Y - 100c</math>, or simply the last 2 digits of <math>Y</math>
- <math>w</math> is the day of week (1 = Sunday,..0 = Saturday)
{|class="wikitable" style="text-align:center;"
|+Table of month offsets (<math>\lfloor 2.6(m + 1)\rfloor</math>)
! Months
| Mar || Apr || May || Jun || Jul || Aug || Sep || Oct || Nov || Dec || Jan || Feb
|-
! Offset
| 10 || 13 || 15 || 18 || 20 || 23 || 26 || 28 || 31 || 33 || 36 || 39
|}
The only difference is one between Zeller's algorithm () and the Disparate Gaussian algorithm (), that is .
: <math> (d + \lfloor(m + 1)2.6\rfloor + y +\lfloor y/4\rfloor + \lfloor c/4\rfloor - 2c) \bmod 7 - (d + \lfloor 2.6m - 0.2\rfloor + y +\lfloor y/4\rfloor + \lfloor c/4\rfloor - 2c) \bmod 7</math>
: <math> = (\lfloor(m + 2 + 1)2.6 - (2.6m - 0.2)\rfloor) \bmod 7 </math> (March = 3 in but March = 1 in )
: <math> = (\lfloor 2.6m + 7.8 - 2.6m + 0.2\rfloor) \bmod 7</math>
: <math> = 8 \bmod 7 = 1</math>
Wang's algorithm
Wang's algorithm for human calculation of the Gregorian calendar is (the formula should be subtracted by 1 if m is 1 or 2 if the year is a leap year)<br />
<math display="block"> w = \left(d - d_0(m) + y_0 - y_1 + \left\lfloor y_0/4-y_1/2\right\rfloor - 2\left(c\bmod 4 \right)\right) \bmod 7,</math>
where
- is the last digit of the year (units)
- is the second last digit of the year (tens)
- is the century, given by <math>c = \lfloor \textit{year}/100 \rfloor</math>
- is the day of the month (1 to 31)
- is the month (January=1,...,December=12)
- is the day of the week (0=Sunday,...,6=Saturday)
- is the null-days function (month offset) with values listed in the following table
{| class="wikitable"
|-
! !!colspan=2|
|-
| 1 || 1 || A day
|-
| 3 || 5 ||rowspan=3| + 2
|-
| 5 || 7
|-
| 7 || 9
|-
| 9 || 3 ||rowspan=2| + 1
|-
| 11 || 12
|-
| 2 || 12 || + 3
|-
| 4 || 2 ||rowspan=5| − 2
|-
| 6 || 4
|-
| 8 || 6
|-
| 10 || 8
|-
| 12 || 10
|}
An algorithm for the Julian calendar can be derived from the algorithm above
<math display="block"> w = \left(d - d_0(m) + y_0 - y_1 + \left\lfloor y_0/4-y_1/2\right\rfloor - c\right) \bmod 7,</math>
where is a doomsday.
{| class="wikitable"
|-
! !!colspan=2|
|-
| 1 || 3 || C day
|-
| 3 || 7 ||rowspan=3| + 4
|-
| 5 || 9
|-
| 7 || 11
|-
| 9 || 5 ||rowspan=2| − 4
|-
| 11 || 7
|-
| 2 || 0 || − 2
|-
| 4 || 4||rowspan=5|
|-
| 6 || 6
|-
| 8 || 8
|-
| 10 || 10
|-
| 12 || 12
|}
Other algorithms
Schwerdtfeger's method
In a partly tabular method by Schwerdtfeger, the year is split into the century and the two digit year within the century. The approach depends on the month. For ,
:<math> c = \left\lfloor\frac{y}{100}\right\rfloor \quad \text{and} \quad g = y - 100 c,</math>
so is between 0 and 99. For ,
:<math> c = \left\lfloor\frac{y-1}{100}\right\rfloor \quad \text{and} \quad g = y - 1 - 100 c.</math>
The formula for the day of the week is
<blockquote>
Take the given date in 4 portions, viz. the number of centuries, the number of years over, the month, the day of the month.
Compute the following 4 items, adding each, when found, to the total of the previous items. When an item or total exceeds 7, divide by 7, and keep the remainder only.
Century-item: For 'Old Style' (which ended 2 September 1752) subtract from 18. For 'New Style' (which began 14 September 1752) divide by 4, take overplus [surplus] from 3, multiply remainder by 2.
Year-item: Add together the number of dozens, the overplus, and the number of 4s in the overplus.
Month-item: If it begins or ends with a vowel, subtract the number, denoting its place in the year, from 10. This, plus its number of days, gives the item for the following month. The item for January is "0"; for February or March, "3"; for December, "12".
Day-item: The total, thus reached, must be corrected, by deducting "1" (first adding 7, if the total be "0"), if the date be January or February in a leap year, remembering that every year, divisible by 4, is a Leap Year, excepting only the century-years, in 'New Style', when the number of centuries is not so divisible (e.g. 1800).
The final result gives the day of the week, "0" meaning Sunday, "1" Monday, and so on.
</blockquote>
Examples:
In 1752, the British Empire abandoned its use of the Old Style Julian calendar upon adopting the Gregorian calendar, which has become today's standard in most countries of the world. For more background, see Old Style and New Style dates.
Methods in computer code
Keith
In the C language expressions below, <code>y</code>, <code>m</code> and <code>d</code> are, respectively, integer variables representing the year (e.g., 1988), month (1–12) and day of the month (1–31).
<syntaxhighlight lang="c">
(d+=m<3 ? y-- : y-2,23*m/9+d+4+y/4-y/100+y/400) % 7
</syntaxhighlight>
In 1990, Michael Keith and Tom Craver published the foregoing expression that seeks to minimize the number of keystrokes needed to enter a self-contained function for converting a Gregorian date into a numerical day of the week. It returns <code>0</code> = Sunday, <code>1</code> = Monday, etc. This expression uses a less cumbersome month component than does Zeller's algorithm.
Shortly afterwards, Hans Lachman streamlined their algorithm for ease of use on low-end devices. As designed originally for four-function calculators, his method needs fewer keypad entries by limiting its range either to A.D. 1905–2099, or to historical Julian dates. It was later modified to convert any Gregorian date, even on an abacus. On Motorola 68000-based devices, there is similarly less need of either processor registers or opcodes, depending on the intended design objective.
Sakamoto's methods
The tabular forerunner to Tøndering's algorithm is embodied in the following K&R C function. With minor changes, it was adapted for other high level programming languages such as APL2. Posted by Tomohiko Sakamoto on the comp.lang.c Usenet newsgroup in 1992, it is accurate for any Gregorian date.
<syntaxhighlight lang=c>
dayofweek(y, m, d) /* 1 <= m <= 12, y > 1752 (in the U.K.) */
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
if ( m < 3 )
{
y -= 1;
}
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
</syntaxhighlight>
It returns <code>0</code> = Sunday, <code>1</code> = Monday, etc.
Sakamoto also simultaneously posted a more obfuscated version:
<syntaxhighlight lang=c>
dow(m,d,y) { y -= m<3; return (y+y/4-y/100+y/400+"-bed=pen+mad."[m]+d) % 7; }
</syntaxhighlight>
This version encodes the month offsets in the string and as a result requires a computer that uses standard ASCII to run the algorithm correctly, reducing its portability. In addition, both algorithms omit type declarations, which is allowed in the original K&R C but not allowed in ANSI C.
(Tøndering's algorithm is, again, similar in structure to Zeller's congruence and Keith's short code, except that the month-related component is . Sakamoto's is somewhere between the Disparate Gaussian and the Schwerdtfeger's algorithm, apparently unaware of the expression form.)
Gauss's method in MATLAB
<syntaxhighlight lang="matlab">% example date input
y1 = 2022;
m1 = 1;
d1 = 1;
month_offset = [0 3 3 6 1 4 6 2 5 0 3 5]; % common year
% offset if y1 leap year
if mod(y1,4)==0 && mod(y1,100)==0 && mod(y1,400)==0
month_offset=[0 3 4 0 2 5 0 3 6 1 4 6]; % leap year
end
% Gregor
weekday_gregor = rem( d1+month_offset(m1) + 5*rem(y1-1,4) + 4*rem(y1-1,100) + 6*rem(y1-1,400),7)
% Julian
weekday_julian = rem(6+5*rem(y1-1,4) + 3*(y1-1),7)</syntaxhighlight>
0: Sunday 1: Monday .. 6: Saturday
Gauss<nowiki>'</nowiki>s method for Gregorian calendar in Python
<syntaxhighlight lang="python3">
from numpy import remainder as rem
def is_leap_year(year: int) -> bool:
"""Determine whether a year is a leap year."""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def day_of_week(y: int, m: int, d: int) -> str:
"""Return day of week of given date as string,
using Gauss's algorithm to find it."""
if is_leap_year(y):
month_offset = (0, 3, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6)[m - 1]
else:
month_offset = (0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5)[m - 1]
y -= 1
wd = int(rem(d + month_offset + 5 * rem(y, 4) + 4 * rem(y, 100) \
+ 6 * rem(y, 400) , 7))
return ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")[wd]
</syntaxhighlight>
See also
- (Has a calendar calculation contest)
References
Notes
Citations
Further reading
External links
- Tøndering's algorithm for both Gregorian and Julian calendars
- "Key Day" method used so as to reduce computation & memorization
- Compact tabular method for memorisation, also for the Julian calendar
- When countries changed from the Julian calendar
- World records for mentally calculating the day of the week in the Gregorian Calendar
- National records for finding Calendar Dates
- World Ranking of Memoriad Mental Calendar Dates (all competitions combined)
- Identify the year by given month, day, day of week.
