Calculate the day of the week

Source: Internet
Author: User
Tags repetition

How to calculate the day of the week?
Slowtiger published on 2009-11-19 08:42:00

How to calculate the day of the week?
--Caille (Zeller) formula
What is the day of the week in history? What day of the future is the week? With regard to this problem, there are many computational formulae (two general formulas and some piecewise formulas), the most famous of which is the Caille (Zeller) formula. namely w=y+[y/4]+[c/4]-2c+[26 (m+1)/10]+d-1

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

The calculated W divided by 7, the remainder is a few weeks. If the remainder is 0, it is Sunday.

Taking October 1, 2049 (National Day of the 100 anniversary) 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)
namely October 1, 2049 (100 Anniversary National Day) is the Week 5.

What is your birthday (at birth, this year, next year)? Give it a try.

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

The derivation of the process: (not interested in reasoning can be omitted to see)

The week system is a system with ancient traditions. It is said that because of the biblical genesis, God used Six
Days of Genesis, the seventh day of rest, so people also take seven days as a cycle to arrange their work and life
Live, and Sunday is the day of rest. From a practical point of view, seven days as a cycle, the length is more appropriate. The
In spite of China's traditional work cycle of 10 days (such as the "Wang Bo" in the Tengwang Pavilion Order), which is
It refers to the official's work every 10th for a period of 10th leave), but later also adopted the Western Week system.

In our daily life, we often meet the question of knowing the day of the week. Sometimes, we still want to know
A day in the history of the Tao. Usually, the effective way to solve this method is to look at the calendar, but we do not always
Take a calendar with you at all times, and it's not always possible to carry a thousands of-year perpetual calendar. If you want to be in computer programming,
It is even more unrealistic to calculate the day of the week and save a perpetual calendar in advance. Is there a way to get through?
What formula has been introduced from the date of the year to the day of the week?

The answer is yes. In fact, we often do so. Let's start with a simple example. For example, know
The May 1, 2004 is Saturday, so May 31, 2004 "World No Tobacco Day" is the week is not difficult to calculate the
To. We can count the fingers from 1st to 31st, and several weeks, and finally the May 31 is Monday.
In fact, using mathematical calculations, you can not break the finger. We know that the week is seven days a cycle, so May 1 is a star
Period of six, seven days after May 8 is also Saturday. On the date, 8-1=7, is a multiple of 7. Similarly, May 15
Day, May 22 and May 29 are also Saturday, their dates and the difference of May 1 are 14, 21 and 28 respectively, also
are multiples of 7. What about May 31? 31-1=30, although not a multiple of 7, but 30 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 of calculating the week: first of all, you need to know the date you want to count.
A definite day before is the day of the week, which is the standard of reckoning, which is the equivalent of a calculated
"Origin". Second, know the difference between the days to be counted and the definite day, with 7 apart from this date
Difference, the remainder represents the days of the week after the determined day of the week. If the remainder is
0, that means the week of the two days is the same. Obviously, if you choose this as the "origin" of the day as Sunday, that
The remainder is just the day of the week, which makes it easier to calculate.

But the direct calculation of the number of days between two days is still cumbersome. Like July 29, 1982 and May 2004.
1st is 7,947 days apart, not all of a sudden can be counted out. It consists of three periods of time: one, July 1982 29
The remaining days of the year after the day; two, 1983-2003 the total number of 21 full days; three, from 2004
The number of days passed from 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 there are 5 leap years in this period. The first and third paragraphs are more troublesome, such as the third paragraph, which requires
The four-month days before May add up, plus the date value, which is 31+29+31+30+1=122 days. Similarly, the first
One need to add up the five months after July, plus the remaining days of July, for a total of 155 days.
So the total number of days separated is 122+7670+155=7947 days.

Think about it, if the date of the "origin" is selected as December 31, then the first period is a
All year round, so that the first and second periods of time can be combined, and the total year is exactly equal to two
The year difference is reduced by one. If the "Origin" date is further selected as December 31, 1 BC (or astronomical
The year of December 31, 00), the total number of the year is exactly the date of the calculation to reduce one. This
Sample simplification, only two periods of time are required: one, the total number of days for so many years; second, the days to calculate are the
The first day of the year. Coincidentally, in accordance with the Gregorian calendar years set, so back, December 31, 1 BC is exactly
Sunday, that is, the remainder of the total number of days divided by 7 is exactly the day of the week. So now the problem is
Only one: How many leap years there are in so many years. This requires an understanding of the set-up rules for the Gregorian calendar.

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

Therefore, for all the year from 1 (or A.D. 00) December 31 to the year y of a date
is equal to the number of leap years in the

[(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400],

[...] Indicates that only the integer portion is taken. The first indicates the number of years to be divisible by 4, and the second indicates the need to remove
The number of years that are divisible by 100, and the third indicates the number of years that are divisible by 400. The reason y wants to subtract one, this
Sample, 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 A.D. 1 (or A.D. 00) December
The number of days between 31st to this day. Divide W with 7, the remainder is a few, this is the day of the week. Like we're here.
Count May 1, 2004:

W = (2004-1) *365 + [(2004-1)/4]-[(2004-1)/100] + [(2004-1)/400] +
= 731702,

731702/7 = 104528 ... 6, the remainder is six, indicating that the day is Saturday. This is in line with the facts.

The above formula (1), although very accurate, but the calculated numbers are too large, very inconvenient to use. Zi
Consider, in fact, the number of days of the interval w is only to get it divided by 7 after the remainder. This inspires us to be
It is not possible to simplify this w value by looking for a smaller number that is the same as its remainder, with the term
, the exact number of weeks can be calculated by finding a smaller positive integer with the same remainder.

Obviously, the reason W is so big is because the first item in the formula (Y-1) *365 is 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 (Y-1) the remainder of *365 divided by 7 is actually
equals the remainder of Y-1 divided by 7. This relationship can be expressed as:

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

Among them, ≡ is the symbol of the number in number theory, mod 7 means to use 7 as a modulus (that is, the divisor)
The number on both sides of the ≡ number is the same. Therefore, it is entirely possible to use (Y-1) instead of (Y-1) *365, so that we can 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 more useful, but it is not the best formula, because the accumulated days of the calculation of D is also more hemp
Bother. Is it possible to calculate the number of months and the date directly? The answer is yes. We might as well 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 28 (=4*7) for this number of days, the remainder of W divided by 7 is not affected. So we'll get another one.

Month: January February March April May June July August September October November December
Days left: 3 0 (1) 3 2 3 2 3 3 2 3 2 3
Common year 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 the remaining days of the five months from March to July are 3,2,3,2 except January and February,
3; The value of the five-month day from August to December is also 3,2,3,2,3, just a repetition. In 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 the repetition. It is precisely because of this law
exist, the cumulative number of common year and leap years can be easily expressed using 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 they want to count; common year i=0, leap years
I=1. The expression for m≥3 needs to be explained: [13* (m+1)/5]-7 is calculated from the second table above
The common year cumulative value, plus (M-1) *28 is the total number of days of all months before the month of the day that you want to count. This is a
A very ingenious way to use the rounding operation to achieve the 3,2,3,2,3 cycle. 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 were to work on it, we would consider January and February as the "1 March" and "1 April" of the previous year, not only still
The formula, and because of this, leap Day became the last "year" (altogether 14 months)
Days, became a part of D, so the impact of the flat leap year is also removed, the formula is simplified into:

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 into:

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

Since both 7 and (M-1) * 282 are divisible by 7, the remainder of W divided by 7 is removed,
The formula becomes:

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

Of course, it is important to note that January and February have been treated as 1 March and 1 April for the previous year, so the days of January and February are calculated
Week, in addition to M to press 13 or 14, the year Y is also reduced by one. For example, January 1, 2004 is Thursday, with this
A formula to calculate, 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 consistent with the reality.

The formula (5) is already the formula of the week from the year, month and day, but it is not the most concise, for the year
There are ways to improve the process. Let's first use this formula to figure out the week of March 1, 1 of the Century, the column
The table is as follows:

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

As you can see, every four centuries, this week repeats. If we put 301 (701,1101,..., 2301)
The week of March 1 is regarded as 2 (according to the definition of the remainder in number theory, 2 and 5 divided by 7 are the same, so you can
To make such a transformation), then the repeating sequence is just a 4,2,0,-2 arithmetic progression. Accordingly, we
You can get the following formula for calculating the week of March 1, 1 per century:

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

, C is the century of the century minus one, MoD represents the modulo operation, that is, to find the remainder. For example, for the March 2001
1st, c=20, then:

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

Substituting the formula (6) into the formula (5), the transformation can be obtained by:

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

Therefore, the formula (5) in the (Y-1) + [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400] These four items, in the calculation
The week of the first year of the century can be replaced with (4-c mod 4) * 2-1. This formula writes
Out is:

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

With a formula for calculating the date and week of the first year of the century, the formula for calculating the date and week of the other years of the century
It's easy to get. Because in a century, the end of the 00 year is the last year, so there is no need to test
Considering the rule of "100 years without leap and 400 years of leap", only the rules of "four years one leap" should be considered. Modeled by the formula (1)
Simplified to the formula (2) method, we can easily from the formula (8) to get a more simple than the equation (5) to calculate any
The day is the formula of the week:

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

In the formula, Y is the post two digit of the year.

If we consider that the modulo operation is not arithmetic, we can further rewrite (4-c mod 4) * 2
into an expression containing only arithmetic. Since century minus one C divided by 4 quotient Q and remainder R have 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)

Bashi (11) substituting (9), resulting in:

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

This formula can be calculated from the century minus one, the end of the year two digits, the month and the number of days to calculate w, and then divided by 7, the remainder is
A few days is the day of the week, the only need to be flexible is to January and February as the previous year of 1 March and 1 April,
Both C and Y are evaluated as the year of the previous year. Therefore, it is generally accepted that it is the best to calculate the day of the week.
Formula. The formula was first developed by the German mathematician Christian Caille (Christian Zeller, 1822-
1899) deduced in 1886, thus known as the Caille formula (Zeller ' s Formula). For the convenience of mental arithmetic,
[M+1]/5] are also often written in [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
Formulas, which are:

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

Note that negative numbers cannot be obtained by the concept of the remainder of the habit, but only by the definition of the remainder in number theory. For convenience
Calculation, we can add a 7 integer to it and make it a positive number, such as adding 70 to get 55.
Divided by 7, the remainder of 6, indicating that this day is Saturday. This is consistent with the actual, and also with the formula (2) calculated the resulting knot
Consistent with the results.

Finally, it is necessary to note that the above formula is based on the Gregorian calendar's set of leap rules to consider
Of For the Julian calendar, Caille also introduced the corresponding formula:

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

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

Add: Calculate the negative number to add 7

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.