jc@atcmp.nl (Jan Christiaan van Winkel) (10/01/90)
I remember some time back someone wanted to know how to compute the day of
week from a date.
In Dr. Dobb's Journal, an elegant solution to this question is printed:
(originally published by Zeller in "Acta Mathematica" #7, Stockholm 1887 (!))
(reprinted without permission)
Given:
J=century (e.g. 19)
K=year (e.g. 90)
q=day of month
m=month BUT:
march=3, april=4,... but january is 13 of the previous year and feb. is 14
of the previous year
for example, for january 6, 1990, J is 19, K is 89, q is 6 and m is 13.
Given these variables, compute the day of week as follows (using integer
arithmetic):
(m + 1) * 26 K J
q + ------------ + K + --- + --- - 2*J
10 4 4
In this formula, all divisions are integer divisions, so discard any
remainders. If the formula gives a negative answer (which is possible),
keep adding 7 until a positive number is the result.
The resulting value is then 'MOD'ded by 7, and a day number results:
0=Saturday, 1=Sunday, 2=Monday and so on...
The remarkable thing about this formula is that NO if's are used to check for
leapyears and stuff like that!
Enjoy!
JC
--
___ __ ____________________________________________________________________
|/ \ Jan Christiaan van Winkel Tel: +31 80 566880 jc@atcmp.nl
| AT Computing P.O. Box 1428 6501 BK Nijmegen The Netherlands
__/ \__/ ____________________________________________________________________