Calendar query algorithm How to calculate how many days a day is the basis of knowledge

Source: Internet
Author: User
Tags arithmetic repetition

How do I calculate the day of the week?
--Caille (Zeller) formula
What is the day of the week in history? What day of the week is the future? There are a number of computational formulae (two general calculation formulae and some piecewise computational formulae), most notably the Caille (Zeller) formula. namely w=y+[y/4]+[c/4]-2c+[26 (m+1)/10]+d-1

The symbolic meanings in the formula are as follows, W: week; C: Century -1;y: Year (two digits); M: Month (M is greater than 3, less than or equal to 14, that is, in Caille formula, the December of a year is counted as 13 and 1 April of the previous year, For example, January 1, 2003 is to be counted as the 1 March 1 of 2002); D: day; [] represents rounding, that is, as long as the integer part. (c is the century number minus one, y is the two digits after the year, M is the month, D is the day number.) January and February are calculated by the 1 March and 1 April of the previous year, when both C and Y are valued in the previous year. )

Calculate the W divided by 7, the remainder is a few is the day of the week. If the remainder is 0, then Sunday.

Taking the October 1, 2049 (100 Anniversary National Day) as an example, the Caille (Zeller) formula is used to calculate the process as follows:
Caille (Zeller) formula: w=y+[y/4]+[c/4]-2c+[26 (m+1)/10]+d-1
=49+[49/4]+[20/4]-2x20+[26x (10+1)/10]+1-1
=54 (divided by more than 7 5)
October 1, 2049 (100 anniversary of the National Day) is the Week 5.

What's the day of your birthday (at birth, this year, next year)? You might as well give it a try.

However, the above formula is only suitable for the situation after October 15, 1582 (the Pope then modified the Julian calendar made by Julius Caesar into the Gregorian calendar, which is used today).

The derivation of a process: (not interested in reasoning can be skipped to see)

The week system is a system with an ancient tradition. It was said that God had spent six days in Genesis and the seventh day of the Bible in Genesis, so that people would spend seven days as a cycle to arrange their work and life, and Sunday was a day off. From a practical point of view, seven days for a cycle, the length is also more appropriate. So although China's traditional work cycle is 10 days (such as the "Wangbo" in the "poetic", which means that officials work every 10th for a cycle, the 10th holiday), but later also adopted the Western Week system.

In daily life, we often encounter the problem of knowing the day of the week. Sometimes, we also want to know the day of the week in history. Usually, the effective way to solve this method is to look at the calendar, but we always do not carry a calendar at any time, more likely to carry a thousands of-year calendar. If you want to calculate the day of the week in computer programming, it is even more unrealistic to put a calendar in advance. At this time is there a way to pass what formula, from the date of the day of the month is the week?

The answer is yes. In fact, we often do so. Let's first give a simple example. For example, know that May 1, 2004 is Saturday, then May 31, 2004 "World No Tobacco Day" is the week is not difficult to calculate. We can count the fingers from 1st to 31st, and a few weeks, and the last one can count out May 31 is Monday.
In fact, the use of mathematical calculations, you can not break fingers. We know that the week is seven days a cycle, so May 1 is Saturday, seven days after the May 8 is also Saturday. On the date, 8-1=7 is just a multiple of 7. Similarly, May 15, May 22 and May 29 are also Saturday, and their dates and May 1 are respectively 14, 21, and 28, and are also multiples of 7. So, what about May 31? 31-1=30, although not a multiple of 7, but 31 divided by 7, the remainder is 2,
This means that the week of May 31 is two days after the week of May 1. Two days after Saturday is Monday.

This simple calculation tells us a basic idea for calculating the week: first, know the day of the week before the day of reckoning, and take the day as a projection, which is the equivalent of a calculated "origin". Second, knowing the difference between the days that you want to count and the days that are fixed, the difference between 7 and the date, the remainder indicates how many days after the week of the day that you want to count. If the remainder is 0, it means that the week of the two days is the same. Obviously, if the day of the "origin" was chosen for Sunday, that
The remainder is exactly equal to the number of days of the week, so the calculation is more convenient.

But it is still cumbersome to calculate the number of days between two days directly. For example, July 29, 1982 and May 1, 2004 between the 7,947-day interval, it is not a sudden can be calculated. It consists of three periods: one, the remaining days of the year after July 29, 1982, and two, 1983-2003 of the 21 full days of the whole year; three, the number of days from 2004 New Year's Day to May 1. The second paragraph is relatively good, it is equal to 21*365+5=7670 days, the reason to add 5, because this period of time has 5 leap years. The first and third paragraphs are more troublesome, such as the third paragraph, need to add up the four months before May, plus the date value, that is, 31+29+31+30+1=122 days. Similarly, the first
One need to add up the number of days after the five months after July plus the number of days left in July, 155 days altogether.
So the total number of days separated is 122+7670+155=7947 days.

Think carefully, if the "origin" date of the day is selected as December 31, then the first period is a whole year, so that the first and second time can be combined, the total year is exactly equivalent to two days of the difference in the year minus one. If the "Origin" day is further selected as the year before December 31, 1 (or the December 31, 00 of the astronomer), the total number of the whole year is just minus one for the years to come. After this simplification, it is only necessary to calculate two periods: one, so many years of the total number of days; Coincidentally, according to the Gregorian calendar years set, so back, the December 31, 1 BC is exactly Sunday, that is, the total number of days calculated by 7 is exactly the remainder of the week. So now the question is
Only one: How many leap years are there in all year. This requires understanding the Gregorian calendar's rule of the leap.

We know that the Gregorian calendar excepting is 365 days, leap year is 366 days. The method of the leap is to be divisible by 4 year in February plus one day, but can be divided by 100 not a leap, can be divisible by 400 and leap. Therefore, like 1600, 2000, 2,400 years is a leap year, and 1700, 1800, 1900, 2,100 years are excepting. 1 BC, the Gregorian calendar is also a leap year.

Therefore, the number of leap years in the year from December 31 (or A.D. 00) to a certain date of the year of 1 is equal to
[(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400],

[...] Represents a part that takes only integers. The first item indicates the need to add the number of years divisible by 4, and the second indicates the need to remove the number of years divisible by 100, and the third indicates the need to add the number of years divisible by 400. The reason y want to subtract one, this
Kind, we get the first formula that calculates the day of the week:

W = (Y-1) *365 + [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400] + D. (1)

where d is the cumulative number of days in this year. The calculated W is the number of days between December 31 (or A.D. 00) between the day of 1 and the date of the year. Use 7 to divide W, the remainder is a few, this day is the days of the week. For example, we'll count May 1, 2004:

W = (2004-1) *365 + [(2004-1)/4]-[(2004-1)/100] + [(2004-1)/400] + (31+29+31+30+1) = 731702,
731702/7 = 104528 ... 6, the remainder is six, indicating that the day is Saturday. This is in line with the fact.

The formula above (1) is very accurate, but the calculated number is too large to be used easily. Think about it, but the number of days in this interval is just to get the remainder after it's divided by 7. This suggests that we can simplify the W value by looking for a smaller number that is the same as its remainder, and, in the term number theory, to find a smaller positive integer that is the same as it is, so that you can calculate the exact day of the week.

Obviously, the reason W is so big is because the first item in the formula (Y-1) *365 too big. In fact

(Y-1) *365 = (Y-1) * (364+1)
= (Y-1) * (7*52+1)
= (Y-1) * 7 + (Y-1),

The first item of this result is a multiple of 7, divided by 7 to 0, so that the remainder of the (Y-1) *365 divided by 7 is actually equal to the remainder of Y-1 divided by 7. This relationship can be expressed as:

(Y-1) *365≡y-1 (mod 7).

Among them, chess is the symbol of the same in number theory, mod 7 means that in the case of using 7 as modulus (that is, divisor), the numbers on both sides of the same number are the same. Therefore, it is perfectly possible to replace (Y-1) *365 with (Y-1) so that we get the famous and most common formula for calculating the day of the week:

W = (Y-1) + [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400] + D. (2)

This formula is much more useful, but it is not the best formula, because the cumulative number of days D is also relatively difficult to calculate
Bother. Is it possible to calculate it directly with the number of months and dates? The answer is yes. We might as well take a look at the
The number of days of the month, the list is as follows:

Month: January February March April May June July August September October November December
Days: 31 28 (29) 31 30 31 30 31 31 30 31 30 31

If you subtract this number of days by 28 (=4*7), it does not affect the remainder value of W divided by 7. So we can get another one.

Month: January February March April May June July August September October November December
Days remaining: 3 0 (1) 3 2 3 2 3 3 2 3 2 3
Excepting cumulative: 3 3 6 8 11 13 16 19 21 24 26 29
Leap Year Cumulative: 3 4 7 9 12 14 17 20 22 25 27 30

If we look closely, we will find that excluding January and February, the remaining days of the five months of March to July are 3,2,3,2,3;8 months to December, and the five-month Sky value is also 3,2,3,2,3, just a repetition. The corresponding cumulative number of days, the difference between the cumulative number of days after January and the cumulative number of days before January minus 28 is this repetition. It is precisely because of this regularity that the cumulative number of excepting and leap years can be easily expressed in mathematical formulas:

╭d (When m=1)
D = {+ D; (when m=2) (3)
╰[* (m+1)/5]-7 + (M-1) * + D + i. (When m≥3)

where [...] Still means to take only the integer part; M and d are the months and days of the day you want to count; excepting i=0, Leap year i=1. The m≥3 expression needs to be explained: [13* (M+1)/5]-7 calculates the excepting cumulative value in the second table above, plus (M-1) The total number of days of all the months preceding the month in which the date was calculated. This is a clever way to use the integer operation to achieve the 3,2,3,2,3 loop. For example, on May 1, 2004, there are:

D = [13 * (5+1)/5]-7 + (5-1) * 28 + 1 + 1
= 122,

This is the cumulative number of days of May 1 in 2004.

If we make a few more modifications, consider January and February as the "1 March" and "1 April" of the previous year, not only does it still conform to this formula, but because of this, the leap day becomes the last of the previous "year" (altogether 14 months) and becomes part of D, so the effect of the flat leap is also removed, and the formula is simplified to:

D = [(m+1)/5]-7 + (M-1) * + D. (3≤M≤14) (4)

The formula that calculates the day of the week can be further simplified to:

W = (Y-1) + [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400] + [(m+1)/5]-7 + (M-1) * + D.

Since the 7 and (M-1) * 282 items can be divisible by 7, the remainder of the W divided by 7 is not changed, and the formula becomes:

W = (Y-1) + [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400] + [(m+1)/5] + D. (5)

Of course, note that January and February have been treated as the 1 March and 1 April of the previous year, so in calculating the days of January and February, in addition to the m to be 13 or 14, the year Y should also be reduced by one. For example, January 1, 2004 is Thursday, with this formula, there are:

W = (2003-1) + [(2003-1)/4]-[(2003-1)/100] + [(2003-1)/400] + [13* (13+1)/5]
+ 1
= 2002 + 500-20 + 5 + 36 + 1
= 2524;
2524/7 = 360 ... 4. This is in line with reality.

The formula (5) is already the formula of the week from year, month, and day, but it is not the most concise, and there are some ways to improve the processing of the year. Let's start by using this formula to figure out the week of the March 1, 1 of each century, as follows:

Year: 1 (401,801,..., 2001) (501,901,..., 2101)
Week: 4 2
Year: 201 (601,1001,..., 2201) (701,1101,..., 2301)
Week: 0 5

It can be seen that every four centuries, this week is repeated. If we think of the number of weeks on the March 1 of the 701,1101,..., 2301) as 2 (as defined in number theory, where 2 and 5 are the same as the remainder of 7, so that this kind of transformation can be done), then this repeating sequence is exactly the arithmetic progression of a 4,2,0,-2. Accordingly, we can get the following formula for calculating the week of the first March 1, 1 of the Century:

W = (4-c mod 4) * 2-4. (6)

In the middle, c is the century of the century number minus one, mod represents modulo operation, that is, to find the remainder. For example, for March 1, 2001, C=20:

W = (4-20 mod 4) * 2-4
= 8-4
= 4.

The formula (6) is passed into the formula (5) and is transformed to be:

(Y-1) + [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400]≡ (4-c mod 4) * 2-1
(mod 7). (7)

Therefore, in the formula (5) (Y-1) + [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400] These four, in the week of the first year of the century, you can use (4-c mod 4) * 2-1 instead. This formula is written in the following way:

W = (4-c mod 4) * 2-1 + [(m+1)/5] + D. (8)

With the formula for calculating the date week of the first year of the century, the formula for calculating the dates of the other years of the century is easy to get. Because in a century, the end of the year 00 is the last year, so there is no need to reconsider the "100 years, 400 years and a leap" rule, only to consider the "four-year-leap" rule. By simplifying the formula (1) to a formula (2), it is easy to get a formula (5) that is simpler to calculate any day of the week:

W = (4-c mod 4) * 2-1 + (y-1) + [Y/4] + [(m+1)/5] + D. (9)

, Y is the back two digits of the year.

If we consider that the modulo operation is not arithmetic, we can further change (4-c mod 4) * 2 into an expression containing only arithmetic. Because the century number minus one C divided by 4 quotient Q and remainder R has the following relationship:

4q + R = C,

where r is the C mod 4, so there are:

r = c-4q
= C-4 * [C/4]. (10)


(4-c mod 4) * 2 = (4-c + 4 * [C/4]) * 2
= 8-2c + 8 * [C/4]
≡[C/4]-2C + 1 (mod 7). (11)

The handles (11) are substituting (9) to obtain:

W = [C/4]-2C + y + [Y/4] + [(m+1)/5] + d-1. (12)

This formula is reduced by the century number one, the end of the year two digits, the number of months and days can be calculated w, divided by 7, the resulting remainder is a few means the day is the week, the only thing to be flexible is to take January and February as the previous year of 1 March and 1 April, C and Y are based on the year's year value. Therefore, it is generally accepted that this is the best formula for calculating any day of the week. This formula was first deduced by the German mathematician Christian Caille (Christian Zeller, 1822-1899) in 1886 and is therefore known as the Caille formula (Zeller ' Formula). For the convenience of Multi-digit, the [(m+1)/5] In the formula is also often written as [num * (M+1)/10].

Now still let's count the week of May 1, 2004, apparently c=20,y=4,m=5,d=1, substituting Caille
Formula, there are:

W = [20/4]-40 + 4 + 1 + [13 * (5+1)/5] + 1-1

Note that negative numbers cannot be based on the concept of the remainder of the habit, can only be based on the definition of the remainder of the number theory. To make it easier to compute, we can add a 7 integer multiple to it, making it a positive number, like adding 70 and getting 55. Divide by 7, 6, which means that the day is Saturday. This is consistent with the reality and is consistent with the calculation of the formula (2).

Finally, the above formula is based on the Gregorian calendar (Gregory) of the set-leap rules to consider. For the Julian calendar, Caille also introduced the corresponding formula is:

W = 5-c + y + [Y/4] + [(m+1)/5] + d-1. (13)

In this way, we finally solved the problem of not looking up the calendar to calculate the day of the week.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.