Day of week routine
Doug Merritt
doug at xdos.UUCP
Tue May 23 00:37:21 AEST 1989
In article <234 at zeek.UUCP> larry at 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
More information about the Comp.lang.c
mailing list