Solved

Finding Day of the Week

Posted on 1998-01-15
6
214 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 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

9 Experts available now in Live!

Get 1:1 Help Now