Thursday, March 31, 2005

Reverend Zeller's Algorithm

Zeller's algorithm is used to compute the day of the week on which a given date will fall (or fell).

Let A, B, C and D denote integer variables that contain the following values:

  • A = the month of the year, with March having the value 1, April the value 2, . . . , December the value 10, and with January and February being counted as months 11 and 12 of the preceding year (in which case, subtract 1 from C).

  • B = the day of the month (1, 2, 3, . . . , 28, 29, 30, 31).

  • C = the year of the century (this would be 95 for the year 1895).

  • D = the century (this would be 18 for the year 1895).

Next let W, X, Y, Z and R denote integer variables, and compute the following values in this order, using integer arithmetic.

     W = (13A - 1) / 5
X = C / 4
Y = D / 4
Z = W + X + Y + B + C - 2D
R = the remainder obtained when Z is divided by 7.
The value of R represents the day of the week, where r = 0 represents Sunday, r = 1 represents Monday,. . . , and r = 6 represents Saturday. If R turns out to have a negative value such as -4, add 7 to it to get a nonnegative value between 0 and 6.

This above was obtained from http://www.cs.niu.edu/~t90hch1/csci210/zeller.htm

0 Comments:

Post a Comment

<< Home