Perpetual Calendar – Weekday computation in your head
To compute the (Gregorian calendar's) weekday for any date, you have a choice between computing more or memorizing some numbers.
Note: In the formulas, the following operators are used:
- / is integer division (divide and ignore the remainder)
- % is the modulus operator (yields the remainder left in an integer division)
Method 1 (formulas only)
The following formula yields 1 for Monday, 2 for Tuesday etc.
( DayOfMonth + Y + Y/4 – Y/100 + Y/400 + 31×M/12 ) % 7
||1 or 2
||Year – 1
||Month + 10
||Month – 2
The side formulas for Y and M basically result in treating January and February as belonging to the previous year. For example, February 2014 yields Y=2013 and M=12, whereas March 2014 yields Y=2014 and M=1.
For similar formulas, have a look at the Wikipedia article on Zeller's congruence.
Method 2 (formula and numbers)
This formula yields 0 for Monday, 1 for Tuesday etc.
( DayOfMonth + MonthCode + Year + Year/4 – 2×(Century%4) – isLeapJanFeb ) % 7
|Mon ||0 ||Aug|
|Tue ||1 ||Feb, Mar, Nov|
|Wed ||2 ||Jun|
|Thu ||3 ||Sep, Dec|
|Fri ||4 ||Apr, Jul|
|Sat ||5 ||Jan, Oct|
|Sun ||6 ||May|
If you care to memorize this, you might prefer the following table for the months:
In the formula,
In order to find the weekday, the resulting number only needs to be looked up in the table already used for the months.
- Century are the first two digits of the given four-digit year
- Year are the latter two digits of the given four-digit year
- MonthCode is the number left of the given month in the table
- isLeapJanFeb is usually 0, but 1 in case the year is a leap year and the month is January or February
(leap years are those that are a multiple of 4 and either no multiple of 100 or a multiple of 400, e.g. 1996 and 2000, but not 2100)
Example Day: 8 May 1945
|(||DayOfMonth ||+||Y ||+||Y/4 ||–||Y/100 ||+||Y/400 ||+||31×M/12||) % 7|
|(||8 ||+||1944 ||+||1944/4 ||–||1944/100 ||+||1944/400 ||+||31×3/12||) % 7|
|(||8 ||+||1944 ||+||486 ||–||19 ||+||3 ||+||9||) % 7 ||=||2431 % 7||=||2|
So the official end of World War II in Europe was a Tuesday. You might check for yourself that it began (1 Sep 1939) on a Friday. Unfortunately with not just one, but 297 weekends in between...
You can check the result of your calculations using this form:
|(||DayOfMonth||+||MonthCode||+||Year||+||Year/4||–||2×(Century%4)||–||isLeapJanFeb||) % 7|
|(||8||+||6||+||45||+||45/4||–||2×(19%4)||–||0||) % 7|
|(||8||+||6||+||45||+||11||–||2×3||–||0||) % 7||=||64 % 7||=||1|
In this calendar, not only are leap years simply defined as all those that are a multiple of 4, but the formula also (Method 2) differs slightly:
( DayOfMonth + MonthCode + Year + Year/4 + 5 – Century – isLeapJanFeb ) % 7
24 Aug 0079, the day Pompeii was buried by Mt. Vesuvius' eruption, was a Tuesday [(24+0+79+19+5–0–0)%7=1]. In the Gregorian calendar, it would have been 22 Aug 0079 (how to compute this is a different story), which unsurprisingly was a Tuesday as well [(22+0+79+19–0–0)%7=1]: When switching from Julian to Gregorian because the former was getting out of sync with the seasons, there was no need to additionally interrupt the cycle of weekdays.