Link to home
Start Free TrialLog in
Avatar of Timbo011598
Timbo011598

asked on

Finding Day of the Week

I hear there is an algorithm for finding the day of the week given the month, day, and year.  What might this algorithm be and does it have a name?  Any web site URLs discussing such an algorithm would also be greatly appreciated!
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

Couldn't find what I was looking for.  I'll outline it.  Hopefully you can program it from there.

The general approach is to :
1.  Know the day of the week of some specific date and then to
2.  Calculate the number of days that have elapsed between the date of interest and the date you know.
3.  Take the modulus of the number of days by 7 and that indicates how many days the day of interest has advanced from the day you know.  (that's clear!)

Let me try an example.

Make the known date a sunday for convenience. Last sunday was 5/11/98. Next monday is 5/19/98.  we are interested in 5/19's day of the week.  The number of days elapsed (easy in this case) is 19-11=8.  take modulo 7 8%7=1.  the day of interest is 1 day ahead of the know date or Sunday + 1 = Monday.  the 19th is a Monday.

more coming.
The next question is do you know how to figure out the number of days between two dates?

What I do is to serialize the dates, that is to turn them into an integer that represents them that each successive data is represented by a successive integer.  Then to calculate the number of days between two dates, convert them both to serial dates and subtract them.  (There are other things you can do with serial dates as well, like calculate a date x days after another date, etc.)

How do you calculate serial dates?

You have to pick a starting date to be represented as 0.  This has to be an early date since you can (conveniently) only deal with dates after this.  I would try to make it a date like a january 1st that occurs as a sunday.  i.e. a date that is the start of a week and a year.  The year could be any year that fits  those conditions that is early enough that all you dates will come after it.

set a days counter to 0.
Then given a date of interest, look at the year and calculate how many whole years have elapsed since the starting date.  The number of days is 365 times the number of years.  Then take number of leap years into account.  (This algorythm will depend on your starting date.  if you need help here, let me know what you decide)    Then calculate the date of interest's number of whole months into the year.  Look up the days this represent in a table.  The add on the date's date in the month.  Finally, if this is a leap year (again algorithm will depend) and you are past the leap day, add one more.

That's the idea.  If you have any questions let me know.  also let me know the range of dates of interest.  Note don't say every date!  Things get messy prior to the 1600's (I think) when there were no leap years and every once in a while (at the Pope's descression) the date was altered.  We're talking about having 5 or 6 leap days in a row one year and then none for a few decades.  Don't go there!
Avatar of ozo
if( month < 3 ){ month += 12; year -= 1; }
jdn = day+1720996+(month+1)*306/10+year*365+year/4-year/100+year/400;
printf("day of week = %d",(jdn+2)%7);

I believe Ozo's formula is to convert a date into a julian (Jdn), which is a type of serial date.  Given the year/4-year/100+year/400 part it should work from the start of leap years to the year 4000. However, I can't figure out the (month+1)*306/10 part.  Is that because the average number of days in a month is 30.6, or something.  If so I'd be surprised that this works--although I'm sure it does--I'm just surprised.
Avatar of Timbo011598

ASKER

Thanks for the prompt answer nietod.  I did some searching as well and discovered some source code.  Also, for in case it comes up again, the name of the algorithm to find the day of the week is Zellar's congruence.  Just thought you'd be interested =)  Thanks again!  --Tim