Weekday calculation

from Wikipedia, the free encyclopedia

The day of the week calculation (also called calendar calculation) aims to determine the day of the week for any date in the past or future . There are various methods that are based on mathematical algorithms .

Even without calculation, the day of the week belonging to a date can be read from permanent or perpetual calendars for any year - or at least for a very long period of time. These calendars can also be used to check the results of the algorithms.

introduction

To calculate the day of the week using a formula, the seven days of the week are numbered consecutively, for example from Monday = 1 to Sunday = 7 (this corresponds to the ISO 8601 standard ). The modulo is required for the calculation : It indicates the remainder that is left over after an integer division (e.g. 15 mod 7 = 1, because 15/7 is 2, remainder 1). The remainder modulo 7 is particularly important for the weekday calculation, which can be interpreted as follows: If the 1st day of a month is a Sunday, then 7 days later, on the 8th day of the month, it is also a Sunday, since 7 mod 7 = 0 The 16th day of the month is also a Monday, since 15 mod 7 = 1.

The basic idea of ​​most algorithms is to start with a date for which the day of the week is known, then the number of days until the desired date is determined. The different lengths of the months and the number of leap years must be taken into account. Regarding leap years, the procedures for the Gregorian and Julian calendars differ . From the number of days since the selected start day, the day of the week can finally be determined using a modulo 7 calculation as described above.

algorithm

The following algorithm was described and derived by Georg Glaeser .

formula

The day of the week is calculated using the following formula:

Here mod is used to denote the modulo operation. If the result is negative, 7 is added (example: -19 mod 7 = -5 + 7 = 2). The square brackets are the Gaussian brackets : They indicate the largest whole number less than or equal to x .

The variables have the following meaning:

d
Current date (1 to 31)
m
Month according to the table below
y
The last two digits of the year, for the months of January and February the last digits of the previous year (for Dec. 1907 7, for Jan 1907 6, for Jan 1900 99)
c
The first two digits of the year, for the months of January and February the first digits of the previous year (for Dec. 1907 19, for Jan 1907 19, for Jan 1900 18)
w
Day of the week according to the following table:
Day Sunday Monday Tuesday Wednesday Thursday Friday Saturday
w 0 1 2 3 4th 5 6th


The month m is given in Julian counting so that the leap month February with its irregularity is at the end of the year. The following table results:

month March April May June July August September October November December January February
m = 1 2 3 4th 5 6th 7th 8th 9 10 11 12
[2.6m - 0.2] mod 7 = 2 5 0 3 5 1 4th 6th 2 4th 0 3


Accordingly, a year runs from March to February. Since m = 11 and m = 12, respectively, for January and February, the year must be reduced by 1 for these two months.

Examples

June 12, 2006:

     d = 12
     m = 4
     y = 6
     c = 20
     A = 12 + [2.6*4 – 0.2] + 6 + [6/4] + [20/4] – 2*20
     A = 12 + 10 + 6 + 1 + 5 - 40
     A = -6
     w = -6 mod 7
     w = 1  entspricht Montag.

January 12, 2006: Here the variable y must be reduced by 1 to 5:

     d = 12
     m = 11
     y = 5
     c = 20
     A = 12 + [2.6*11 – 0.2] + 5 + [5/4] + [20/4] – 2*20
     A = 12 + 28 + 5 + 1 + 5 - 40
     A = 11
     w = 11 mod 7
     w = 4  entspricht Donnerstag.

January 1, 2000: Here the variable y has to be reduced to 99 and c to 19:

     d = 1
     m = 11
     y = 99
     c = 19
     A = 1 + [2.6*11 – 0.2] + 99 + [99/4] + [19/4] – 2*19
     A = 1 + 28 + 99 + 24 + 4 - 38
     A = 118
     w = 118 mod 7
     w = 6  entspricht Samstag.

Derivation of the formula

The derivation takes place in four steps:

1. Formula that is only valid for one month

     (1)   w = (d + k1) mod 7

w is the day of the week you are looking for, d is the date of the day (1–31). k 1 is a correction summand that specifies for each month on which weekday the previous month ended. Mod 7 indicates the smallest positive remainder when dividing by 7, the result can only be in the range from 0 to 6. Example calculation: 22: 7 = 3 remainder 1, so 22 mod 7 = 1.


Example
To calculate the weekday of August 17, 2007 you have to know that July 2007 ended with a Tuesday, i.e. d = 17 and correction summand k 1 = 2. You calculate:

     w = (17 + 2) mod 7 = 19 mod 7 = 5
     5 entspricht Freitag.

2. Formula that is valid for a whole year

Now you have a different formula for each month. To arrive at a formula that is valid for one year, first set March as the first month of the (Julian) year ( m = 1). With this Julian month counting you avoid the problem that the irregular leap day February 29 is in the middle of the year and "disturbs" or complicates the formula. Since the months begin with different days of the week, instead of the correction summand k 1, a "step function" s is required , which supplies the correct weekday of the previous month for each month. To this end, it is examined how the weekday of March 1st "jumps on" to every other beginning of the month:

Change of month ... Month number Jump around ... summed up s (m) =
from March to April m = 2 3 days 3 days 3 mod 7
from April to May m = 3 2 days 5 days 5 mod 7
from May to June m = 4 3 days 8 days 1 mod 7
from June to July m = 5 2 days 10 days 3 mod 7
from July to August m = 6 3 days 13 days 6 mod 7
from August to September m = 7 3 days 16 days 2 mod 7
from September to October m = 8 2 days 18 days 4 mod 7
from October to November m = 9 3 days 21 days 0 mod 7
from November to December m = 10 2 days 23 days 2 mod 7
from December to January m = 11 3 days 26 days 5 mod 7
from January to February m = 12 3 days 29 days 1 mod 7
(from February to March) m = 1 0 or 1 d 29 or 30 d 1 or 2


In total, the beginning of the month jumps from March 1st to February 1st by 29 weekdays, in the 11 monthly jumps so an average of 29/11 ≈ 2.6 weekdays.

The step function s sought should therefore provide the values ​​in column s (m) for m . This is what the term stands for

     [2,6 m] mod 7

Here, 2.6 is multiplied by m , the decimal places are "cut off" using the Gaussian brackets [] and then mod 7 is reduced. Since the results are all too high by 2, 2 is subtracted:

     ([2,6 m] – 2) mod 7

Then the values ​​of m = 5 and m = 10 are still 1 too large. You have to change the formula so that the results are only corrected in these two places: To do this, the results in the Gaussian brackets of these two months of July and December must be reduced by 1, but the other results must not be changed. With m = 5 and m = 10, the value within the Gaussian brackets always results in an integer, with the other months a value that is at least 0.2 above an integer. So you subtract a value greater than 0 and less than or equal to 0.2; usually 0.2 is chosen:

     s(m) = ([2,6 m - 0,2] – 2) mod 7

If you insert s (m) instead of the correction summand k 1 into the above formula (1), you get the weekday formula that is valid for one year:

     (2)   w = (d + [2,6 m - 0,2] - 2 + k2) mod 7

The correction summand k 2 indicates the weekday from the day before March 1 of the year in question.


Example
The following weekday formula for 2007 is valid from March 1, 2007 to February 29, 2008. Since the day before March 1, 2007 is a Wednesday, k 2 = 3 applies :

    w = (d + [2,6 m - 0,2] - 2 + 3) mod 7 = (d + [2,6 m - 0,2] +1) mod 7

3. Formula that is valid for an entire century

Now you have to vary formula (2) so that it is valid for several years. To do this, the day of the week must be "pushed forward" by one day each year, since a normal year consists of 52 weeks and one day. In every leap year, the day of the week must advance by 2 days.

y stands for the last two digits of the year (for 1992 y = 92). Since there is a leap year every four years, y / 4 has to be added. For 1995, for example, this would result in 95/4 = 23.75. But because you need an integer, you put the term in Gaussian brackets : [y / 4]; this "cuts" the digits after the comma. So 23.75 in Gaussian brackets becomes 23, because in the 20th century there were exactly 23 leap years before 1995.

If you now insert the term y + [y / 4] instead of the correction summand k 2 in the formula (2), you get a weekday formula that is valid for a century:

     (3)   w = (d + [2,6 m - 0,2] - 2 + y + [y/4] + k3) mod 7

The correction summand k 3 indicates the day of the week on which the previous century ended.


Examples
2000 was a leap year. The day before March 1, 2000, February 29, 2000, was a Tuesday, i.e. k 3 = 2. Consequently, the weekday formula for the 21st century is:

      w = (d + [2,6 m - 0,2] - 2 + y +[y/4] + 2) mod 7

It is valid from March 1, 2000 to February 28, 2100.


1900 was not a leap year. The day before March 1, 1900, February 28, 1900 was a Wednesday, i.e. k 3 = 3. Consequently, the weekday formula for the 20th century is:

      w = (d + [2,6 m - 0,2] - 2 + y +[y/4] + 3) mod 7

It is valid from March 1, 1900 to February 29, 2000.

4. General formula

Now formula (3) still has to be made compatible for several centuries. In the Gregorian calendar , years that are divisible by 100 but not by 400 are not leap years . On the other hand, leap years remain divisible by 400, like all other years, but not divisible by 100 years.

The number of these special leap years that occur every 400 years is calculated using the formula [c / 4], where c stands for the first two digits in the four-digit year, from c / 4 the Gaussian bracket [] is again formed. In 1963 ( c = 19, y = 63) z. B. [19/4] = 4.

Furthermore, it must be taken into account that with the jumps "over the century" the value of y + [y / 4] changes from 99 + 24 = 123 = 4 mod 7 to 00 + 0 = 0 every time. However, this value must not be 0, but must be 5, since the day of the week advances by 1 with the year. This 5 must be added at every turn of the century. Since 5 = −2 mod 7, the jump from +5 corresponds to just −2. Therefore, the zeroing of y + [y / 4] and the added day can be compensated for in the century leaps with the summand −2c.

In the formula (3) one can now replace the correction summand k 3 as follows:

      k3 = [c/4] − 2c + k4

The formula still has to be "adjusted" by choosing k 4 . The previous formula gives a Wednesday for October 15, 1582, but it was a Friday. So k 4 = 2, which, together with the previous −2, happens to result in 0. Overall, you get the weekday formula you are looking for:

     w = (d + [2,6 m – 0,2] + y + [y/4] + [c/4] – 2c) mod 7

programming

The following script can be used directly in the LabVIEW programming environment in a Formula Node , or with modifications in other programming languages. It uses Gregorian instead of Julian dates (January = 1, December = 12) and already includes the decrease in the year number y for January and February; Year and month numbers can therefore be taken over unchanged from the Gregorian date. The script outputs the weekday number w in German standard: Monday = 1, Sunday = 7. In addition, the results of the modulo operator "%" are corrected if it does not follow the usual mathematical convention that the sign of the result is the same as that of the divisor is. "%" is the modulo operator here, "floor ()" the Gaussian bracket for rounding off. The parameter y contains the complete year, not just the last two digits; As above, m and d are the number of the month and the day.

     if (m < 3) y = y - 1;
     w = ((d + floor (2.6 * ((m + 9) % 12 + 1) - 0.2) + y % 100 + floor (y % 100 / 4) + floor (y / 400) - 2 * floor (y / 100) - 1) % 7 + 7) % 7 + 1;

A method of mental arithmetic

Assuming that a date is composed according to the following scheme:

,

where the first two digits represent the four-digit year and the last two, the following five values ​​of a date can be calculated:

Daily number

The daily number : it is the day of the month modulo 7:

Month number

The monthly number to remember:

month Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
0 3 3 6th 1 4th 6th 2 5 0 3 5


In January it starts with zero. The digits of the others result from the remainder of the previous month:

January has 31 days, and 31 mod 7 = 3, therefore February has the 3 (remainder of January) + 0 = 3.
February has 28 days, and 28 mod 7 = 0, and for March this 0 is added to the 3 of February, and that results in 3.
March has 31 days, 31 mod 7 = 3, and 3 + 3 = 6, so the reference number for April is 6.
April has 30 days, 30 mod 7 = 2 and 6 + 2 = 8, and 8 mod 7 = 1, so the reference number for May is 1 etc.

If you have forgotten a month digit, you can work it out again.

Year number

For the year digit, the year in the century, i.e. only the last two digits of the year, is added to the rounded whole number result of division by 4 of the same number. This sum is then divided modulo 7:

The following figures result for the year in the century as an example:

year 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14th 15th 16 17th 18th 19th 20th 21st 22nd 23 24 25th 26th 27 28
Digit 0 1 2 3 5 6th 0 1 3 4th 5 6th 1 2 3 4th 6th 0 1 2 4th 5 6th 0 2 3 4th 5 0


The counting is always 1, in leap years by 2, after the 6 it goes back to the 0. These digits repeat every 28 years, so ..28 had the same number as ..56 and ..84.


Example for 1950:
50 + (50 div 4) = 50 + 12 = 62, that works in 7 exactly 8 times = 56 and 6 remains as the remainder. The year number for 1950 is therefore the 6. Since the year number of ..50 is the is the same as that of ..78 or ..22 (78 = 50 + 28, 22 = 50 - 28), you can also calculate the year digit in this case with smaller numbers: The year digit of 1950 is the same as that of 1922, i.e. 22 + (22 div 4 = 5) = 27 = 3 · 7 + 6; Here too, of course, you get the 6 as the year number.

Century digit

The first two digits of the year are . The formula for the century digit is:

Accordingly, the century digit is:

0 for all years starting with 15 *), 19, 23, 27.
2 for all years starting with 18, 22, 26.
4 for all years starting at 17, 21, 25.
6 for all years starting with 16, 20, 24.

Note: The century digit is not identical as to whether it belongs to the century . The 20th century, for example, covers the years 1901 to 2000, so that the last year of the 20th century, unlike the other 99 years, has the century digit 20.

*) Only applies from October 15, 1582, the first day of the Gregorian calendar. Previously, by papal order, 10 days had been lost, so that dates before October 15, 1582 cannot be calculated using this method. For countries in which the Gregorian calendar was introduced later, a different, later limit date applies accordingly, e.g. B. for Turkey March 1, 1917, before which this method no longer works.

The 400 year cycle in the Gregorian calendar has 146,097 days, and this number is divisible by 7. The assignment of the days of the week to the dates is repeated every 400 years. B. has the same days of the week as 1604, 2404, 2804, etc.

Leap year correction

So far we have added the leap day to the whole year, so the calculation is only correct from March 1st. If the date is in January or February of a leap year , is (or 6, since modulo 7 is always calculated), otherwise .

Result

If you add these 5 numbers and determine the remainder by dividing by 7, you get the day of the week :

The day of the week can now be determined using the numerical result :

0 1 2 3 4th 5 6th
Day So Mon Tuesday Wed do Fr. Sat


With all these additions, one can always calculate mod 7, i.e.

instead of adding a 1, you can subtract 6
instead of adding a 2, you can subtract 5
instead of adding a 3, you can subtract 4
instead of adding a 4, you can subtract 3
instead of adding a 5, you can subtract 2
instead of adding a 6, you can subtract 1

You only have to deal with numbers from 0 to 6, so that fingers are sufficient as a calculation aid.

Examples

July 14, 1789

  1. 14 mod 7 = 0
  2. July has the number 6
  3. ..89 reference number 6
  4. 17 .. reference number 4
  5. no leap year correction, i.e. 0
(0 + 6 + 6 + 4 + 0) mod 7 = 2.

The Bastille was stormed on a Tuesday.

May 23, 1949

  1. 23 mod 7 = 2
  2. May 1
  3. ..49 5
  4. 19 .. 0
  5. 0
(2 + 1 + 5 + 0 + 0) mod 7 = 1.

The Federal Republic of Germany was founded on a Monday.

January 18, 1892

  1. 18 mod 7 = 4
  2. Jan 0
  3. ..92 3
  4. 18 .. 2
  5. Leap year correction! 6th
(4 + 0 + 3 + 2 + 6) mod 7 = 1

Oliver Hardy was born on a Monday.

November 9, 1989

  1. 9 mod 7 = 2
  2. Nov. 3
  3. ..89 6
  4. 19 .. 0
  5. 0
(2 + 3 + 6 + 0 + 0) mod 7 = 4

The fall of the wall was on a Thursday.

Simplifications in mental arithmetic

With the formula described, it is in principle possible to calculate the day of the week in the header. In order to speed up the calculation and to achieve such results as described below in the "Sport" section, the following simplifications are possible, among others:

The sequence of the steps (day → month → year → century → leap year) does not have to be strictly adhered to, since the associative law applies to the addition . The order can be changed as you wish. It makes sense to calculate the steps in the order that the intermediate results result in 7. If it is recognized during the calculation that, for example, and is, then these two steps can be omitted (since 7 mod 7 = 0). With some experience in using this formula, one can recognize such situations and execute the formula accordingly in a different order.

In addition, not all data is equally important in most situations. When used in everyday life, it is far more common to calculate the day of the week for data in the current or the next year than in years ago. If that's the case, it makes sense to calculate and memorize the sum of and mod 7 for, for example, the years 2019 and 2020. This can simplify the application in everyday life, since overall less has to be learned by heart - nevertheless the application period of the formula is limited to the learned years.

Result control

The perpetual calendar shown is a simple and safe method of checking the results .

With the kind permission of the tax advisor calendar (reprinted annually), Verlag CH Beck, Munich

history

The first computational method for determining the day of the week comes from Carl Friedrich Gauß (1777–1855). In a handwritten note, he described a formula that can be used to calculate the day of the week of January 1st of any year, which he himself never published. Rather, he was interested in the related and much more complex calculation of the Easter date , a problem that had preoccupied mathematicians since the dawn of Christianity. For this purpose he published the so-called Gaussian Easter formula .

Towards the end of the 19th century there was greater interest in the weekday calculation. In 1882, the mathematician and theologian Christian Zeller published a formula (see Zeller's congruence ) that could be used to calculate the day of the week on any date. Another publication on this topic is by Lewis Carroll in the journal Nature in 1887. In it Carroll writes: “I'm not a high-speed calculator myself and on average it takes me about 20 seconds to answer a question asked; But I have no doubt that a real high-speed calculator would not even need 15 seconds to answer. "

Further procedures

Sports

Calendar arithmetic is a discipline at the world championships in mental arithmetic , which have been held every two years since 2004 . As many days of the week as possible for dates between 1600 and 2100 must be determined within one minute. The best of 2 attempts is rated.

World champion in calendar arithmetic
year Surname country Result
2004 Matthias Kesselschläger Germany 33
2006 Matthias Kesselschläger Germany 35
2008 Jan van Koningsveld Germany 40
2010 Yusnier Viera Cuba 48
2012 Myagmarsuren Tuuruul Mongolia 57
2014 Marc Jornet Sanz Spain 64
2016 Georgi Georgiev Bulgaria 66
2018 Marc Jornet Sanz Spain 71

The world record is 140 data per minute and was set in 2018 by Yusnier Viera from the United States (formerly Cuba).

Individual evidence

  1. Georg Glaeser: The mathematical toolbox. Applications in nature and technology. 3. Edition. Elsevier, Munich 2008, ISBN 3-8274-1618-3 , see p. 408
  2. ^ Lewis Carroll, "To Find the Day of the Week for any Given Date," in "Nature," Volume 35, March 31, 1887, p. 517
  3. Martin Gardner: Mathematical Carnival, Chapter: "Tricks of the quick calculator"
  4. http://www.recordholders.org/de/events/worldcup/
  5. http://www.recordholders.org/en/records/dates.html

Web links

Commons : Perpetual calendars  - collection of images, videos and audio files