Determination of the day of the week

Last updated

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.

Contents

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. 18 mod 7 = 4). [a]

StandardMondayTuesdayWednesdayThursdayFridaySaturdaySundayUsage examples
ISO 8601 1234567%_ISODOWI%, %@ISODOWI[]% (4DOS); [1] DAYOFWEEK() (HP Prime) [2]
0123456
2345671 %NDAY OF WEEK% (NetWare, DR-DOS [3] ); %_DOWI%, %@DOWI[]% (4DOS) [1]
1234560HP financial calculators

The basic approach of nearly all of the methods to calculate the day of the week begins by starting from an "anchor date": a known pair (such as 1 January 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 day of the week.

One standard approach is to look up (or calculate, using a known rule) the value of the first day of the week of a given century, look up (or calculate, using a method of congruence) an adjustment for the month, calculate the number of leap years since the start of the century, and then add these together along with the number of years since the start of the century, and the day number of the month. Eventually, one ends up with a day-count to which one applies modulo 7 to determine the day of the week of the date. [4]

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 is quite viable: the former is easier for calculators and 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 unreasonable dates will produce erroneous results.

Corresponding days

Every seventh day in a month has the same name as the previous:

Day of
the month
d
00 07 14 21 280
01 08 15 22 291
02 09 16 23 302
03 10 17 24 313
04 11 18 254
05 12 19 265
06 13 20 276

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. January always corresponds to May of the previous year. In leap years or common years preceding leap years, January corresponds to September and December of the following year. In common years preceding common years, January corresponds to April and July of the following year.

February corresponds to March and November in common years and August in leap years. February always corresponds to June of the previous year. In leap years or common years preceding leap years, February corresponds to May of the following year. In common years preceding common years, February corresponds to August of the following year.

March and November correspond to each other in all years and February in common years. In common years, they correspond to June of the previous year. In leap years, they corresponds to September and December of the previous year. In years preceding common years, they corresponds to August of the following year. In years preceding leap years, they corresponds to May of the following year.

April and July correspond to each other in all years and January in leap years. In common years, they correspond to January and October of the previous year. In leap years, they correspond to May of the previous year. In years preceding common years, they correspond to September and December of the following year. In years preceding leap years, they correspond to June of the following year.

September and December correspond to each other in all years. In common years, they correspond to April and July of the previous year. In common years following leap years, they also correspond to January of the previous year. In leap years, they correspond to January and October of the previous year. In years preceding common years, they correspond to June of the following year. In years preceding leap years, they correspond to March and November of the following year.

August doesn’t correspond to any other month in a common year but corresponds to February in leap years. In common years, August corresponds to March and November of the previous year. In common years following common years, August also corresponds to February of the previous year. In years preceding common years, August corresponds to May of the following year. In years preceding leap years, August corresponds to October of the following year.

October corresponds to January in a common year but doesn’t correspond to any other month in a leap year. In common years, October corresponds to May of the previous year. In leap years, October corresponds to August of the previous year. In years preceding common years, October correspond to April and July of the following year. In years preceding leap years, October corresponds to September and December of the following year.

May and June never correspond to any other month. May always corresponds to January of the following year. In years preceding common years, May corresponds to October of the following year. In years preceding leap years, May corresponds to April and July of the following year. In common years, May corresponds to August of the previous year. In leap years, May corresponds to February, March, and November of the previous year. June always corresponds to February of the following year. In years preceding common years, June corresponds to March and November of the following year. In years preceding leap years, June corresponds to August of the following year. In common years, June corresponds to September and December of the previous year. In leap years, June corresponds to April and July of the previous year.

In the months table below, corresponding months have the same number, a fact which follows directly from the definition.

Common yearsLeap yearsAll yearsm
JanOct0
May1
FebAug2
FebMar Nov3
Jun4
Sept Dec5
JanApr July6

Corresponding years

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.

2021 is a common year starting on Friday, which means that it corresponds to the 2010 calendar year. The first two months of 2021 also correspond to the first two months of 2016. The next occurrence of all months of 2021 is in 2027. 2022 is a common year starting on Saturday, which means that it corresponds to the 2011 calendar year. The last ten months of 2022 also correspond to the last ten months of 2016. The next occurrence of the first two months of 2022 is in 2028. The next occurrence of the last ten months of 2022 is in 2033. 2023 is a common year starting on Sunday, which means that it corresponds to the 2017 calendar year. The next occurrence of the first two months of 2023 is in 2034. The next occurrence of the last ten months of 2023 is in 2028. 2024 is a leap year starting on Monday, which means that it corresponds to the 1996 calendar year. The first two months of 2024 correspond to the first two months of 2018. The last ten months of 2024 correspond to the last ten months of 2019. The next occurrence of the first two months of 2024 is in 2029. The next occurrence of the last ten months of 2024 is in 2030.

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 Wednesday was 2020 and the next occurrence will be 2048. Likewise, the next common years starting on Friday will be 2027, 2038, and then 2049. Both of these statements are true unless a leap year is skipped, but that will not happen until 2100. Additionally, with the exception of skipped leap years, each leap year falls 2 days of the week earlier than the previous one. A given day falls on a certain day of the week four times in a 28 year cycle, with repeated days every 6 years, 5 years, 6 years, then 11 years. For example, Christmas fell on a Wednesday in 2013, 2019, and 2024 and the next occurrence of a Wednesday Christmas is in 2030. After that, Christmas will not fall on a Wednesday again until 2041.

For details see the table below.

Year of the
century mod 28
y
00 06 12 17 230
01 07 12 18 241
02 08 13 19 242
03 08 14 20 253
04 09 15 20 264
04 10 16 21 275
05 11 16 22 006

Notes:

Corresponding centuries

Julian century
mod 700
Gregorian century
mod 400 [b]
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 [c]
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, (so called 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 "r0" through "r6" 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.

Hundreds of YearsRemaining Year DigitsMonth
DoW
#
Julian
(r ÷ 7)
Gregorian
(r ÷ 4)
r5191620r000 06 17 2328 34 45 5156 62 73 7984 90JanOctSa0
r41815 19 r301 07 12 1829 35 40 4657 63 68 7485 91 96MaySu1
r31702 13 19 2430 41 47 5258 69 75 8086 97FebAugM2
r21618 22 r203 08 14 2531 36 42 5359 64 70 8187 92 98FebMarNovTu3
r115 09 15 20 26 37 43 48 54 65 71 76 82 93 99JunW4
r01417 21 r104 10 21 2732 38 49 5560 66 77 8388 94SepDecTh5
r61305 11 16 2233 39 44 5061 67 72 7889 95JanAprJulF6

For determination of the day of the week (1 January 2000, Saturday)

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.

Example: What is the day of the week of 27 January 8315?

8315−6300=2015, 2015−100=1915, 1915/100=19 remainder 15, 19×7=133, 133/9=14 remainder 7. 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. 6 + 4 + 0 + 27 + 50 − 14 = 73. 73/7=10 remainder 3. Day of week = Tuesday.

Dominical Letter

To find the Dominical Letter, calculate the day of the week for either 1 January or 1 October. If it is Sunday, the Dominical Letter is A, if Saturday B, and similarly backwards through the week and forwards through the alphabet to Monday, which is 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"

This is an artefact of recreational mathematics. See doomsday rule for an explanation.

Check the result

Use this table for finding the day of the week without any calculations.

IndexMon
A
Tue
B
Wed
C
Thu
D
Fri
E
Sat
F
Sun
G
Perpetual Gregorian and Julian calendar
Use Jan and Feb for leap years
Date letter in year row for the letter in century row

All the C days are doomsdays

Julian
century
Gregorian
century
Date01
08
15
22
29
02
09
16
23
30
03
10
17
24
31
04
11
18
25

05
12
19
26

06
13
20
27

07
14
21
28

12 1916 20AprJulJanGABCDEF010712182935404657636874859196
13 20SepDecFGABCDE0213192430414752586975808697
14 2117 21JunEFGABCD030814253136425359647081879298
15 22FebMarNovDEFGABC0915202637434854657176829399
16 2318 22AugFebCDEFGAB0410212732384955606677838894
17 24MayBCDEFGA0511162233394450616772788995
18 2519 23JanOctABCDEFG0617232834455156627379849000
[Year/100]Gregorian
century
20
16
21
17
22
18
23
19
Year mod 100
Julian
century
19
12
20
13
21
14
22
15
23
16
24
17
25
18

Examples:

26 December 1893 (GD)

December is in row F and 26 is in column E, so the letter for the date is C located in row F and column E. 93 (year mod 100) is in row D (year row) and the letter C in the year row is located in column G. 18 ([year/100] in the Gregorian century column) is in row C (century row) and the letter in the century row and column G 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 G. The letter in the century row (13) and column G is E, so the day of the week is Friday.

1 January 2000 (GD)

January 1 corresponds to G, G in the year row (00) corresponds to F in the century row (20), and F corresponds to Saturday.

A pithy formula for the method: "Date letter (G), letter (G) is in year row (00) for the letter (F) in century row (20), 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 d that has passed since a date of known day of the week D. The day of-the-week is then given by (D + d) mod 7, conforming to whatever convention was used to encode D.

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. [5] He never published it. It was finally included in his collected works in 1927. [6] 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 A, month number M, date number D.
Output
Day of year.
Procedure
  1. First determine the day-of-week d1 of 1 January.
    • For a Gregorian calendar, the weekday is [5]
        (1 + 5((A1) % 4) + 4((A1) % 100) + 6((A1) % 400)) % 7. Alternatively, set C = A \ 100, Y = A % 100, and the value is
        (1 + 5((Y1)%4) + 3(Y1) + 5(C%4)) % 7.
    • For a Julian calendar, the weekday is
        (6 + 5((A1)%4) + 3(A1)) % 7 or
        (6 + 5((Y1) % 4) + 3(Y1) + 6C) % 7.
  2. Now determine the month-related offset m by using the lookup table with M.
  3. Return d = (d1 + m + D) % 7.
Table of month offsets
MonthsJanFebMarAprMayJunJulAugSepOctNovDec
Common years033614625035
Leap years4025036146

The above procedure can be condensed into a single expression for the Gregorian case:
  (D + m + 5((A1)%4) + 4((A1)%100) + 6((A1)%400))%7

Worked example

For year number 2000, A − 1 = 1999, Y − 1 = 99 and C = 19, the weekday of 1 January is

= (1 + 5(1999%4) + 4(1999%100) + 6(1999%400))%7
= (1 + 1 + 4 + 0) % 7
= 6
= (1 + 5(99%4) + 3 × 99 + 5(19%4))%7
= (1 + 1 + 3 + 1)%7
= 6 = Saturday.

The weekdays for 30 April 1777 and 23 February 1855 are

= (30 + 6 + 5(1776%4) + 4(1776%100) + 6(1776%400))%7
= (2 + 6 + 0 + 3 + 6)%7
= 3 =Wednesday

and

= (6 + 23 + ⌈2.6 × 12⌉ + 5(1854%4) + 4(1854%100) + 6(1854%400))%7
= (6 + 2 + 4 + 3 + 6 + 5)%7
= 5 = Friday.

Explanation and notes

The algorithm for the day-of-week of 1 Jan can be proven using modulo arithmetic. The main point is that because 365 % 7 = 1, each year adds 1 day to the progression. The rest is adjustment for leap year. The century-based versions have 36525 % 7 = 6.

The table of month offsets show a divergence in February due to the leap year. A common technique (later used by Zeller) is to shift the month to start with March, so that the leap day is at the tail of the counting. In addition, as later shown by Zeller, the table can be replaced with an arithmetic expression.

This formula was also converted into graphical and tabular methods for calculating any day of the week by Kraitchik and Schwerdtfeger. [6] [7]

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 [8]

where

  • d is the day of the month (1 to 31)
  • m is the shifted month (March = 1,...,February = 12)
  • Y is the year unless m is 11 = January or 12 = February which are considered part of the preceding year, giving Y = year 1
  • c is the century given by
  • y is the year relative to the century, given by , or simply the last 2 digits of Y
  • w is the day of the week (0 = Sunday,...,6 = Saturday)
Table of month offsets ()
MonthsMarAprMayJunJulAugSepOctNovDecJanFeb
Offset257101215182023252831

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. [9] The formula for the Gregorian calendar is
where

Table of month offsets ()
MonthsMarAprMayJunJulAugSepOctNovDecJanFeb
Offset101315182023262831333639

The only difference is one between Zeller's algorithm (Z) and the Disparate Gaussian algorithm (G), that is ZG = 1 = Sunday.

(March = 3 in Z but March = 1 in G)

Wang's algorithm

Wang's algorithm [10] 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)
where

m
11A day
35m + 2
57
79
93m + 1
1112
212m + 3
42m − 2
64
86
108
1210

An algorithm for the Julian calendar can be derived from the algorithm above where is a doomsday.

m
13C day
37m + 4
59
711
95m − 4
117
20m − 2
44m
66
88
1010
1212

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 m 3,

so g is between 0 and 99. For m = 1,2,

The formula for the day of the week is [6]

where the positive modulus is chosen. [6]

The value of e is obtained from the following table:

m123456789101112
e032503514624

The value of f is obtained from the following table, which depends on the calendar. For the Gregorian calendar, [6]

c mod 40123
f0531

For the Julian calendar, [6]

c mod 70123456
f5432106

Lewis Carroll's method

Charles Lutwidge Dodgson (Lewis Carroll) devised a method resembling a puzzle, yet partly tabular in using the same index numbers for the months as in the "Complete table: Julian and Gregorian calendars" above. He lists the same three adjustments for the first three months of non-leap years, one 7 higher for the last, and gives cryptic instructions for finding the rest; his adjustments for centuries are to be determined using formulas similar to those for the centuries table. Although explicit in asserting that his method also works for Old Style dates, his example reproduced below to determine that "1676, February 23" is a Wednesday only works on a Julian calendar which starts the year on January 1, instead of March 25 as on the "Old Style" Julian calendar.

Algorithm: [11]

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.

Examples: [11]

1783, September 18

17, divided by 4, leaves "1" over; 1 from 3 gives "2"; twice 2 is "4". 83 is 6 dozen and 11, giving 17; plus 2 gives 19, i.e. (dividing by 7) "5". Total 9, i.e. "2" The item for August is "8 from 10", i.e. "2"; so, for September, it is "2 plus 31", i.e. "5" Total 7, i.e. "0", which goes out. 18 gives "4". Answer, "Thursday".

1676, February 23

16 from 18 gives "2" 76 is 6 dozen and 4, giving 10; plus 1 gives 11, i.e. "4". Total "6" The item for February is "3". Total 9, i.e. "2" 23 gives "2". Total "4" Correction for Leap Year gives "3". Answer, "Wednesday".

Dates before 1752 would in England be given Old Style with 25 March as the first day of the new year. Carroll's method however assumes 1 January as the first day of the year, thus he fails to arrive at the correct answer, namely "Friday".

Had he noticed that 1676, February 23 (with 25 March as New Year's Day) is actually 1677, February 23 (with 1 January as New Year's Day), he would have accounted for differing year numbers—just like George Washington's birthday differs—between the two calendars. Then his method yields:

1677 (corrected), February 23

16 from 18 gives "2" 77 is 6 dozen and 5, giving 11; plus 1 gives 12, i.e. "5". Total "7" The item for February is "3". Total 10, i.e. "3" 23 gives "2". Total "5". Answer, "Friday".

It is noteworthy that those who have republished Carroll's method have failed to point out his error, most notably Martin Gardner. [12]

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, y, m and d are, respectively, integer variables representing the year (e.g., 1988), month (1–12) and day of the month (1-31).

(d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7

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. [13] It returns 0 = Sunday, 1 = 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. [14]

Sakamoto's methods

The tabular forerunner to Tøndering's algorithm is embodied in the following K&R C function. [15] With minor changes, it was adapted for other high level programming languages such as APL2. [16] Posted by Tomohiko Sakamoto on the comp.lang.c Usenet newsgroup in 1992, it is accurate for any Gregorian date. [17] [18]

dayofweek(y,m,d)/* 1 <= m <= 12, y > 1752 (in the U.K.) */{staticintt[]={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;}

It returns 0 = Sunday, 1 = Monday, etc.

Sakamoto also simultaneously posted a more obfuscated version:

dow(m,d,y){y-=m<3;return(y+y/4-y/100+y/400+"-bed=pen+mad."[m]+d)%7;}

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 int 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 31*m/12. Sakamoto's is somewhere between the Disparate Gaussian and the Schwerdtfeger's algorithm, apparently unaware of the expression form.)

Gauss's method in MATLAB

% example date inputy1=2022;m1=1;d1=1;month_offset=[033614625035];% common year% offset if y1 leap yearifmod(y1,4)==0&&mod(y1,100)==0&&mod(y1,400)==0month_offset=[034025036146];% leap yearend% Gregorweekday_gregor=rem(d1+month_offset(m1)+5*rem(y1-1,4)+4*rem(y1-1,100)+6*rem(y1-1,400),7)% Julianweekday_julian=rem(6+5*rem(y1-1,4)+3*(y1-1),7)

0: Sunday 1: Monday .. 6: Saturday

Gauss''s method for Gregorian calendar in Python

fromnumpyimportremainderasremdefis_leap_year(year:int)->bool:""" Determine whether a year is a leap year. """returnyear%4==0and(year%100!=0oryear%400==0)defday_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 '''ifis_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-=1wd=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]

See also

Related Research Articles

In mathematics and computer programming, exponentiating by squaring is a general method for fast computation of large positive integer powers of a number, or more generally of an element of a semigroup, like a polynomial or a square matrix. Some variants are commonly referred to as square-and-multiply algorithms or binary exponentiation. These can be of quite general use, for example in modular arithmetic or powering of matrices. For semigroups for which additive notation is commonly used, like elliptic curves used in cryptography, this method is also referred to as double-and-add.

A leap year is a calendar year that contains an additional day compared to a common year. The 366th day is added to keep the calendar year synchronised with the astronomical year or seasonal year. Since astronomical events and seasons do not repeat in a whole number of days, calendars having a constant number of days each year will unavoidably drift over time with respect to the event that the year is supposed to track, such as seasons. By inserting ("intercalating") an additional day—a leap day—or month—a leap month—into some years, the drift between a civilization's dating system and the physical properties of the Solar System can be corrected.

The Julian day is the continuous count of days since the beginning of the Julian period, and is used primarily by astronomers, and in software for easily calculating elapsed days between two events.

<span class="mw-page-title-main">Nisan</span> 1st month of the Hebrew calendar

Nisan in the Babylonian and Hebrew calendars is the month of the barley ripening and first month of spring. The name of the month is an Akkadian language borrowing, although it ultimately originates in Sumerian nisag "first fruits". In the Hebrew calendar it is the first month of the ecclesiastical year, called the "first of the months of the year", "first month", and the month of Aviv בְּחֹ֖דֶשׁ הָאָבִֽיב ḥōḏeš hāʾāḇîḇ). It is called Nissān in the Book of Esther. It is a month of 30 days. In the year 2024, 1 Nisan will occur on 9 April. Counting from 1 Tishrei, the civil new year, it would be the seventh month, but in contemporary Jewish culture, both months are viewed as the first and seventh simultaneously, and are referred to as one or the other depending on the specific religious aspects being discussed.

<span class="mw-page-title-main">Floor and ceiling functions</span> Nearest integers from a number

In mathematics, the floor function is the function that takes as input a real number x, and gives as output the greatest integer less than or equal to x, denoted x or floor(x). Similarly, the ceiling function maps x to the least integer greater than or equal to x, denoted x or ceil(x).

A common year starting on Friday is any non-leap year that begins on Friday, 1 January, and ends on Friday, 31 December. Its dominical letter hence is C. The most recent year of such kind was 2021 and the next one will be 2027 in the Gregorian calendar, or, likewise, 2022 and 2033 in the obsolete Julian calendar, see below for more. This common year is one of the three possible common years in which a century year can end on, and occurs in century years that yield a remainder of 100 when divided by 400. The most recent such year was 1700 and the next one will be 2100.

<span class="mw-page-title-main">Date of Easter</span>

As a moveable feast, the date of Easter is determined in each year through a calculation known as computus. Easter is celebrated on the first Sunday after the Paschal full moon. Determining this date in advance requires a correlation between the lunar months and the solar year, while also accounting for the month, date, and weekday of the Julian or Gregorian calendar. The complexity of the algorithm arises because of the desire to associate the date of Easter with the date of the Jewish feast of Passover which, Christians believe, is when Jesus was crucified.

Dominical letters or Sunday letters are a method used to determine the day of the week for particular dates. When using this method, each year is assigned a letter depending on which day of the week the year starts. The Dominical letter for the current year 2024 is GF.

A common year starting on Thursday is any non-leap year that begins on Thursday, 1 January, and ends on Thursday, 31 December. Its dominical letter hence is D. The most recent year of such kind was 2015 and the next one will be 2026 in the Gregorian calendar or, likewise, 2021 and 2027 in the obsolete Julian calendar, see below for more.

<span class="mw-page-title-main">Perpetual calendar</span> Calendar designed to look up the day of the week for a given date

A perpetual calendar is a calendar valid for many years, usually designed to look up the day of the week for a given date in the past or future.

<span class="mw-page-title-main">Doomsday rule</span> Way of calculating the day of the week of a given date

The Doomsday rule, Doomsday algorithm or Doomsday method is an algorithm of determination of the day of the week for a given date. It provides a perpetual calendar because the Gregorian calendar moves in cycles of 400 years. The algorithm for mental calculation was devised by John Conway in 1973, drawing inspiration from Lewis Carroll's perpetual calendar algorithm. It takes advantage of each year having a certain day of the week upon which certain easy-to-remember dates, called the doomsdays, fall; for example, the last day of February, April 4 (4/4), June 6 (6/6), August 8 (8/8), October 10 (10/10), and December 12 (12/12) all occur on the same day of the week in any year.

In number theory, the integer square root (isqrt) of a non-negative integer n is the non-negative integer m which is the greatest integer less than or equal to the square root of n,

Zeller's congruence is an algorithm devised by Christian Zeller in the 19th century to calculate the day of the week for any Julian or Gregorian calendar date. It can be considered to be based on the conversion between Julian day and the calendar date.

In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, called the modulus of the operation.

Shanks' square forms factorization is a method for integer factorization devised by Daniel Shanks as an improvement on Fermat's factorization method.

The ISO week date system is effectively a leap week calendar system that is part of the ISO 8601 date and time standard issued by the International Organization for Standardization (ISO) since 1988 and, before that, it was defined in ISO (R) 2015 since 1971. It is used (mainly) in government and business for fiscal years, as well as in timekeeping. This was previously known as "Industrial date coding". The system specifies a week year atop the Gregorian calendar by defining a notation for ordinal weeks of the year.

<span class="mw-page-title-main">Ordinal date</span> Date written as number

An ordinal date is a calendar date typically consisting of a year and an ordinal number, ranging between 1 and 366, representing the multiples of a day, called day of the year or ordinal day number. The two parts of the date can be formatted as "YYYY-DDD" to comply with the ISO 8601 ordinal date format. The year may sometimes be omitted, if it is implied by the context; the day may be generalized from integers to include a decimal part representing a fraction of a day.

A calendrical calculation is a calculation concerning calendar dates. Calendrical calculations can be considered an area of applied mathematics. Some examples of calendrical calculations:

The Gregorian calendar is the calendar used in most parts of the world. It went into effect in October 1582 following the papal bull Inter gravissimas issued by Pope Gregory XIII, which introduced it as a modification of, and replacement for, the Julian calendar. The principal change was to space leap years differently so as to make the average calendar year 365.2425 days long, more closely approximating the 365.2422-day 'tropical' or 'solar' year that is determined by the Earth's revolution around the Sun.

In modular arithmetic, Barrett reduction is a reduction algorithm introduced in 1986 by P.D. Barrett.

References

Notes

  1. To illustrate this in detail it is useful to visualise a calendar depicting a month beginning on a Sunday, i.e. the 1st of the month is a Sunday). Counting forward seven days brings us to the 8th, which is also a Sunday. Counting forward another ten days brings us to the 18th, which is a Wednesday. If instead we were to start on Wednesday the 4th (three days after Sunday the 1st), count forward seven days to Wednesday the 11th (three days after Sunday the 8th), then count forward another seven days, we would end up on Wednesday the 18thagainthree days after Sunday the 15th, which itself falls exactly two weeks (two Sundays) after Sunday the 1st.
  2. The numbers in the first column are proleptic - the Gregorian calendar was not devised till 1582. See the note beneath the table.
  3. The Julian century beginning 1 BC would also appear on this line of the table (to the left of 700) but there is no space to include it.

Citations

  1. 1 2 Brothers, Hardin; Rawson, Tom; Conn, Rex C.; Paul, Matthias R.; Dye, Charles E.; Georgiev, Luchezar I. (2002-02-27). 4DOS 8.00 online help.
  2. "HP Prime - Portal: Firmware update" (in German). Moravia Education. 2015-05-15. Archived from the original on 2016-11-05. Retrieved 2015-08-28.
  3. Paul, Matthias R. (1997-07-30). NWDOS-TIPs Tips & Tricks rund um Novell DOS 7, mit Blick auf undokumentierte Details, Bugs und Workarounds. Release 157 (in German) (3rd ed.). Archived from the original on 2016-11-04. Retrieved 2014-08-06. (NB. NWDOSTIP.TXT is a comprehensive work on Novell DOS 7 and OpenDOS 7.01, including the description of many undocumented features and internals. It is part of the author's yet larger MPDOSTIP.ZIP collection maintained up to 2001 and distributed on many sites at the time. The provided link points to a HTML-converted older version of the NWDOSTIP.TXT file.)
  4. Richards, E. G. (1999). Mapping Time: The Calendar and Its History . Oxford University Press. ISBN   978-0-19-850413-9.
  5. 1 2 Gauss, Carl F. (1981). "Den Wochentag des 1. Januar eines Jahres zu finden. Gueldene Zahl. Epakte. Ostergrenze.". Werke. herausgegeben von der Koeniglichen Gesellschaft der Wissenschaften zu Goettingen (2nd ed.). Hildesheim: Georg Olms Verlag. pp. 206–207. ISBN   978-3-48704643-3.
  6. 1 2 3 4 5 6 Schwerdtfeger, Berndt E. (2010-05-07). "Gauss' calendar formula for the day of the week" (PDF) (1.4.26 ed.). Retrieved 2012-12-23.
  7. Kraitchik, Maurice (1942). "Chapter 5: The calendar". Mathematical recreations (2nd revised [Dover] ed.). Mineola: Dover Publications. pp. 109–116. ISBN   978-0-48645358-3.
  8. Rosen, Kenneth H. (2011). Elementary Number Theory and Its Applications. Addison Wesley. pp. 134–137. ISBN   978-0321500311.
  9. Stockton, J. R. (2010-03-19). "The Calendrical Works of Rektor Chr. Zeller: The Day-of-Week and Easter Formulae". Merlyn. Archived from the original on 2013-07-29. Retrieved 2012-12-19.
  10. Wang, Xiang-Sheng (March 2015). "Calculating the day of the week: null-days algorithm" (PDF). Recreational Mathematics Magazine. No. 3. p. 5.
  11. 1 2 Dodgson, C.L. (Lewis Carroll). (1887). "To find the day of the week for any given date". Nature, 31 March 1887. Reprinted in Mapping Time, pp. 299-301.
  12. Martin Gardner. (1996). The Universe in a Handkerchief: Lewis Carroll's Mathematical Recreations, Games, Puzzles, and Word Plays, pages 24-26. Springer-Verlag.
  13. Michael Keith; Tom Craver. (1990). The ultimate perpetual calendar? Journal of Recreational Mathematics, 22:4, pp.280-282.
  14. The 4-function Calculator; The Assembly of Motorola 68000 Orphans; The Abacus. gopher://sdf.org/1/users/retroburrowers/TemporalRetrology
  15. "Day-of-week algorithm NEEDED!" news:1993Apr20.075917.16920@sm.sony.co.jp
  16. APL2 IDIOMS workspace: Date and Time Algorithms, line 15. ftp://ftp.software.ibm.com/ps/products/apl2/info/APL2IDIOMS.pdf%5B%5D (2002)
  17. "Date -> Day of week conversion". Google newsgroups:comp.lang.c. December 1992. Retrieved 2020-06-21.
  18. "DOW algorithm". Google newsgroups:comp.lang.c. 1994. Retrieved 2020-06-21.

Further reading

  • Hale-Evans, Ron (2006). "Hack #43: Calculate any weekday" . Mind performance hacks (1st ed.). Beijing: O'Reilly. pp.  164–169. ISBN   9780596101534.
  • Thioux, Marc; Stark, David E.; Klaiman, Cheryl; Schultz, Robert T. (2006). "The day of the week when you were born in 700 ms: Calendar computation in an autistic savant". Journal of Experimental Psychology: Human Perception and Performance. 32 (5): 1155–1168. doi:10.1037/0096-1523.32.5.1155. PMID   17002528.
  • Treffert, Darold A. (2011-10-12). "Why calendar calculating?". Islands of genius : the bountiful mind of the autistic, acquired, and sudden savant (1. publ., [repr.]. ed.). London: Jessica Kingsley. pp. 63–66. ISBN   9781849058735.