Solved

Finding Day of the Week

Posted on 1998-01-15
6
196 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

16 Experts available now in Live!

Get 1:1 Help Now