Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!dciem!nrcaer!cognos!garyp From: garyp@cognos.UUCP (Gary Puckering) Newsgroups: comp.unix.wizards Subject: Re: Date conversion question Message-ID: <416@cognos.UUCP> Date: Sun, 1-Mar-87 19:54:31 EST Article-I.D.: cognos.416 Posted: Sun Mar 1 19:54:31 1987 Date-Received: Tue, 3-Mar-87 18:35:52 EST References: <222@devon.UUCP> <2590003@hpisod2.HP.COM> <9945@teknowledge-vaxc.ARPA> Reply-To: garyp@cognos.UUCP (Gary Puckering) Organization: Cognos Incorporated, Ottawa, Canada Lines: 54 In article <9945@teknowledge-vaxc.ARPA> mkhaw@teknowledge-vaxc.UUCP (Michael Khaw) writes: >Is there a clever formula that will give you the day of the week (as a number >between 1 and 7 or 0 and 6) given year, month, and date? This question came up in September of last year. Here is the response I submitted at that time: There is an formula known as Zeller's Congruence which can be used to calculate the day of the week given any date. I found this somewhere, years ago when I was a teenager, memorized it and never forgot it. I know it works for any year after 1700, maybe even earlier. I can't remember where it came from (I did well to remember the algorithm!). Since it uses only integer addition and division, and one comparison operation, its fairly cheap to implement. Here it is: Let k be the day of the month m be the month (March=1, April=2, ... December=10, January and February are months 11 and 12 of the previous year) C be the century D be the year of the century (adjusted according to m) Z be the day of the week (0=Sunday, 6=Saturday) Then: Z = { (26m - 2)//10 + k + D + D//4 + C//4 - 2C } mod 7 where // represents integer division with truncation Example: for February 28, 1986 k = 28 y = 1986 m = 2 - 2 = 0 if m<1 then m=m+12, y=y-1 C = 19 D = 85 Therefore: Z = { (26*12)//10 + 28 + 85 + 85//4 + 19//4 - 2*19} mod 7 = { 31 + 28 + 85 + 21 + 4 - 38 } mod 7 = { 131 } mod 7 = 5 (Friday) -- Gary Puckering 3755 Riverside Dr. Cognos Incorporated Ottawa, Ontario decvax!utzoo!dciem! (613) 738-1440 CANADA K1G 3N3 nrcaer!cognos!garyp