C language detailed Jiezzaile (Zeller) formula calculates the day of the week is extremely convenient

Source: Internet
Author: User

--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 notable of which is Caille (ZellerFormula Thatw=y+[y/4]+[c/4]-2c+[26 (m+1)/10]+d-1

The symbols in the formula have the following meanings,WWeekC: Century-1;y: Year (two digits);mMonthsmGreater than or equal3, less than or equal to -, that is, in the Caille formula, a year's1、2The month is considered the last year -、 -Months to calculate, for example2003Years1Month1Day to be seen as2002Years of -Month1Days calculation);DDay[]Represents rounding, that is, as long as the integer part.(CIs the century minus one,yis two digits after the year,Mis the month,Dis the number of days.1Months and2Month to the last year -Months and -Months to count, thenCAndyare evaluated by the previous year.)

Figure it out.WDividing7, the remainder is a few days of the week. If the remainder is0, then Sunday.

To2049YearsTenMonth1Day -Annual National Day) For example, with Caille (ZellerThe formula is calculated as follows:
Caille (ZellerFormulaw=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 (Dividing7More thanW)That2049YearsTenMonth1Day -Annual National Day) is the week5。

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

However, the above formula is only suitable for1582YearsTenMonth theAfter the day (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 )

The week system is a system with ancient traditions. It is said that because the Bible · Genesis stipulates that God has 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
Although the traditional work cycle in China is 10 days (as Wang Bo in the Tengwang Pavilion sequence)"10 Holidays", 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 2004 year 5 month 1 The Day is Saturday, so 2004 year 5 month Day"World No Tobacco Day"It's not hard to figure out the day of the week.
come on. We can break our fingers from 1 .number of days todays, and in a few weeks, you can count 5 .month the day is Monday.
in fact, using mathematical calculations, you can not break the finger. We know that week is a seven-day cycle, so 5 month 1 Day is a star
period six, seven days after the 5 month 8 The day is also Saturday. On the date,8-1=7 , exactly 7 the multiple. Similarly,5 month 15
Day,5 month Day and 5 month the day is also Saturday, their date and 5 month 1 the difference between the days is , upand , Also
It's all 7 .the multiple. So 5 month Where's the day? 31-1=30 , though not 7 in multiples, but divide by 7 ., the remainder is 2 ,
This means that5 month Day of the week, was at 5 month 1 two days after the day of the week. 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 Point". Second, know the difference between the days to be counted and the definite day, with 7 In addition to this date
Difference, the remainder represents the days of the week after the determined day of the week . If the remainder is
0 , it means that the two days of the week are the same. Obviously, if you take this as"origin Point"the day of 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 1982 .year 7 month Day and 2004 year 5 Month
1 between Days 7947 God, it's not going to work out all of a sudden. It consists of three periods of time: One,1982 year 7 Month 29
The number of days remaining in the year after the day; two,1983-2003 .all the days of the 21 full year; three, from 2004 years
New Year's Day to 5 month 1 days after the day. The second paragraph is relatively good, it equals 21*365+5=7670 days, the reason to add
5 because there are 5 of them in this period.a leap year. The first and third paragraphs are more troublesome, such as the third paragraph, which requires
5 four months before the month is added together, plus the date value, i.e. 31+29+31+30+1=122 days. Similarly, the first
A paragraph needs to put 7 five months after the month add up, plus 7 the remaining days of the month are 155 .days.
so the total number of days apart is 122+7670+155=7947 days.

think carefully, if you put"origin Point"the date of the day is selected asmonth Day, then the first period of time is also 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 further"origin Point"the day is 1 BC .year month Day (or astronomy
A.D. 0 used by the schoolyear month Day), the total number of the whole year is just the year of the calculated day minus 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, 1 BCyear month Day is exactly
Sunday, that is to say, the total number of days divided by 7 .The remainder 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, leap years are 366 days. The method of placing a leap is to be able to be 4 the divisible year is
2 month plus one day, but can bethe non-leap of divisible, can bea leap in the division of the divide. So, like the, the,2400
years is a leap year, and 1700 ,1800 ,1900 ,2100 years are common year. 1 BCthe Gregorian calendar is also a leap year.

thus, for 1 BC fromyear (or A.D. 0 years) month the year of the day to a certain date Y All the year between
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 need to add 4 the number of years divisible, and the second indicates the need to remove
was the number of years divisible, and the third indicates the need to addthe number of years that are divisible. The reason Y to lose 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 for this day. Figure out the W is 1 BC .year (or A.D. 0 years) Month
toThe number of days between the day and the day . Put the W with 7 In addition, the remainder is a few, this day is the days of the week. Like we're here.
Count 2004 year 5 month 1 Day:

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.

/ * Caille (Zeller) formula calculates the day of the week W: Week, C: (first two digits of the year), Y: Year (two digits after the year), 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 considered as the last year of 13, For example, January 1, 2003 should be considered as the 2002 1 March 1 to calculate); D: The day calculates the W divided by 7, the remainder is a few weeks. */
#include <stdio.h>
int main ()
int y,c,m,d,w,year,month,day;
printf ("Please enter a date: \ n");
scanf ("%d%d%d", &year,&month,&day);
y=year%100;//year 2015 is 15
c=year/100;//The first two digits of the year are 2015
if (M ==1 | | m = = 2) {//Judging whether the month is 1 or 2
m + = 12;//December to be considered as the last year of 13, 1 April to calculate
formula for w=y+y/4+c/4-2*c+13* (m+1)/5+d;//Caille formula
While (W < 0) W + = 7;//ensures that the remainder is positive
printf ("The date entered is the day of the Week%d", W);

C Language Detail Jiezzaile (Zeller) formula calculates the day of the week is extremely convenient

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: info-contact@alibabacloud.com 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.