Day of week algorithm wanted for "C"

Rocky Moore moorer at jacobs.CS.ORST.EDU
Tue Nov 21 02:54:29 AEST 1989


In article <1031 at icus.islp.ny.us> lenny at icus.islp.ny.us (Lenny Tropiano) writes:
>I need a function that accepts a month, a day, and a year ... and returns
>the day-of-the-week (ie. Sunday, Monday, etc..)

Here is a little piece of code I pulled out of the time routine I use.  It will
return a value 0-6 where 0=Sunday, 1=Monday, ect...

--- CUT HERE ---

/* Find and return day of week (0-6) */

int day_of_week(int year, int month, int day)
{
     int offsets[13] = { 0,0,3,3,6,1,4,6,2,5,7,3,5 };
     int dw;

     dw=6+year+((year+3)/4)+offsets[month]+day;
     if( ((year%4) ==0) && (month > 2)) dw++;
     if( (year==0) && (month < 3)) dw++;
     dw=(dw%7);
     return(dw);
}

--- END ---

This should handle what you want.  Hope it helps.

Rocky Moore     moorer at jacobs.cs.orst.edu



More information about the Comp.lang.c mailing list