views:

48

answers:

3

Does anyone know a way to convert a month, year, and day into the day's name for any year? Example:

function convert(day, year, month)
...
return "Monday"
end

Thanks in advance!

+1  A: 

You can use the following method:

This method uses codes for different months and years to speed up the calculation of the day of the week. You might even be able to memorize the codes. We'll use December 16, 2482 as an example.

Take the last 2 digits of the year. In our example, this is 82.

Divide by 4, and drop any remainder. 82 / 4 = 20, remainder 2, so we think "20."

Add the day of the month. In our example, 20 + 16 = 36.

Add the month's key value, from the following table. Jan Feb Mar Apr May June July Aug Sept Oct Nov Dec 1 4 4 0 2 5 0 3 6 1 4 6

The month for our example is December, with a key value of 6. 36 + 6 = 42.

If your date is in January or February of a leap year, subtract 1. We're using December, so we don't have to worry about this step.

Add the century code from the following table. (These codes are for the Gregorian calendar. The rule's slightly simpler for Julian dates.) 1700s 1800s 1900s 2000s 4 2 0 6

Our example year is 2482, and the 2400s aren't in the table. Luckily, the Gregorian calendar repeats every four hundred years. All we have to do is add or subtract 400 until we have a date that is in the table. 2482 - 400 = 2082, so we look at the table for the 2000s, and get the code 6. Now we add this to our running total: 42 + 6 = 48.

Add the last two digits of the year. 48 + 82 = 130.

Divide by 7 and take the remainder. This time, 1 means Sunday, 2 means Monday, and so on. A remainder of 0 means Saturday.

How to calculate the day of the week

Michael Goldshteyn
+1 for writing it out
Brad
A: 

Wikipedia seems to have an article on it:

http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week

Tim Čas
A: 

A quickl google for "day of week from date algorithm" showed up this Wikipedia article

But depending on the dates you need to work with, beware the strange history of Gregorian calendar adoption

Paul