NOTE: In this algorithm January and February are counted as months 13 and 14 of the previous year. E.g. if it is 2 February 2010, the algorithm counts the date as the second day of the fourteenth month of 2009 For an ISO week date Day-of-Week d, use
Implementation in software
The formulas rely on the mathematician's definition of modulo division, which means that −2 mod 7 is equal to positive 5. Unfortunately, the way most computer languages implement the remainder function, −2 mod 7 returns a result of −2. So, to implement Zeller's congruence on a computer, the formulas should be altered slightly to ensure a positive numerator. The simplest way to do this is to replace by and by. So the formulas become: for the Gregorian calendar, and for the Julian calendar. One can readily see that, in a given year, March 1 is a good test date; and that, in a given century, the best test year is that which is a multiple of 100. Zeller used decimal arithmetic, and found it convenient to use J and K in representing the year. But when using a computer, it is simpler to handle the modified year, which is during January and February: for the Gregorian calendar, and for the Julian calendar.
Analysis
These formulas are based on the observation that the day of the week progresses in a predictable manner based upon each subpart of that date. Each term within the formula is used to calculate the offset needed to obtain the correct day of the week. For the Gregorian calendar, the various parts of this formula can therefore be understood as follows:
represents the progression of the day of the week based on the day of the month, since each successive day results in an additional offset of 1 in the day of the week.
represents the progression of the day of the week based on the year. Assuming that each year is 365 days long, the same date on each succeeding year will be offset by a value of.
Since there are 366 days in each leap year, this needs to be accounted for by adding another day to the day of the week offset value. This is accomplished by adding to the offset. This term is calculated as an integer result. Any remainder is discarded.
Using similar logic, the progression of the day of the week for each century may be calculated by observing that there are 36524 days in a normal century and 36525 days in each century divisible by 400. Since and, the term : accounts for this. To avoid negative numbers, this term can be replaced with : with equivalent results.
The term which also can be replaced by adjusts for the variation in the days of the month. Starting from January, the days in a month are. February's 28 or 29 days is a problem, so the formula rolls January and February around to the end so February's short count will not cause a problem. The formula is interested in days of the week, so the numbers in the sequence can be taken modulo 7. Then the number of days in a month modulo 7 would be. Starting in March, the sequence basically alternates 3, 2, 3, 2, 3, but every five months there are two 31-day months in a row. The fraction 13/5 = 2.6 and the floor function have that effect; the denominator of 5 sets a period of 5 months.
The overall function,, normalizes the result to reside in the range of 0 to 6, which yields the index of the correct day of the week for the date being analyzed.
The reason that the formula differs for the Julian calendar is that this calendar does not have a separate rule for leap centuries and is offset from the Gregorian calendar by a fixed number of days each century. Since the Gregorian calendar was adopted at different times in different regions of the world, the location of an event is significant in determining the correct day of the week for a date that occurred during this transition period. This is only required through 1929, as this was the last year that the Julian calendar was still in use by any country on earth, and thus is not required for 1930 or later. The formulae can be used proleptically, but with care for years before year AD 1 because implementation of modulo operators and euclidean divisions may truncate integers to the wrong direction. To accommodate this, one can add a sufficient multiple of 400 Gregorian or 700 Julian years, and subtract 1 for BC year numbers. They are in fact proleptic right up to 1 MarchAD 4 owing to mismanagement in Rome in the period since the calendar was put into effect on 1 January45 BC.
Examples
For 1 January 2000, the date would be treated as the 13th month of 1999, so the values would be: So the formula evaluates as. However, for 1 March 2000, the date is treated as the 3rd month of 2000, so the values become so the formula evaluates as.