Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!sun-barr!apple!motcsd!xdos!doug From: doug@xdos.UUCP (Doug Merritt) Newsgroups: comp.lang.c Subject: Re: Day of week routine Keywords: Does anybody have this? Message-ID: <322@xdos.UUCP> Date: 22 May 89 14:37:21 GMT References: <234@zeek.UUCP> Reply-To: doug@xdos.UUCP (Doug Merritt) Distribution: usa Organization: Hunter Systems, Mountain View CA (Silicon Valley) Lines: 26 In article <234@zeek.UUCP> larry@zeek.UUCP (Larry Podmolik) writes: >I need a C function that, given a date, would return what day of the >week it fell on. ... Try this: ((year * 1.25) + (day-of-the-year) + constant) modulo 7 ...where day-of-the-year is obtained by adding day-of-month to sum of days in each month prior to month-of-year, and I forget the exact value of constant, but it's trivial to obtain by using the formula on today's date, and seeing what adjustment is needed (it really is a constant). An adjustment for the switchover from the Julian to the Gregorian calendar may be needed depending on the era and country of interest. A variation on this formula (involving a table of constants for each month in place of day of the year) can be used to easily *mentally* calculate day of the week. All calculations can be done modulo 7, which makes it easier. The factor of 1.25 is what makes it work despite leap years. Doug -- Doug Merritt {pyramid,apple}!xdos!doug Member, Crusaders for a Better Tomorrow Professional Wildeyed Visionary