Solved

Finding Day of the Week

Posted on 1998-01-15
6
205 Views
Last Modified: 2010-04-10
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!
0
Comment
Question by:Timbo011598
  • 4
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1178387
answer coming.  I have to find it.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178388
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178389
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!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

by:ozo
ID: 1178390
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);

0
 
LVL 22

Expert Comment

by:nietod
ID: 1178391
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.
0
 

Author Comment

by:Timbo011598
ID: 1178392
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now