Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Finding Day of the Week

Posted on 1998-01-15
6
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 300 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

618 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