Sunday, October 3, 2010

What day of the week?

Ever wondered what day of the week some event occurred (or will occur)? What day of the week were you born, for example? Here’s a formula to figure it out (this will only work for the modern Gregorian Calendar – dates after 1752 in the United States).

Let’s use July 4, 1776 as an example to illustrate the process.

First we need to calculate some variables:

d = Day of the month

m = Month number. Start in March with 3, April is 4, May is 5, up to December which is 12, January is 13, and February is 14 (this is to deal with leap years)

y = The year of the century (the last two digits of the year). If the date’s in January or February, they’re treated as the 13th and 14th month of the previous year so subtract 1 from this value (e.g. February 5, 2001 would have m=14 and y = 00).

c = The century (the first two digits of the year

For example, for July 4, 1776, the values are:

d = 4
m = 7
y = 76
c = 17

Then use the formula:

w = {(d) + [(m + 1) 26 / 10] + (y) + (y / 4) + (c / 4) – (2 c)} mod 7

A couple of notes. First, all of the division is integer division. This is where you divide the two numbers and only keep the integer part (the part of the number to the left of the decimal point) and throw away the decimal part. For example, (14 / 5) = 2 in integer division (it’s really 2.8 but we toss out the 0.8 part). For the above equation, there will be no decimals, just whole numbers (integers).

Another oddity is the mod 7. Mod refers to modulo and is the opposite of integer division. It’s division where you only keep the remainder. For (14 mod 5), it’s (14 / 5) which is 2 with 4 left over so the answer is 4.

Let’s run through this formula with my example numbers:

w = {(d) + [(m + 1) 26 / 10] + (y) + (y / 4) + (c / 4) – (2 c)} mod 7

Let’s insert my values for July 4, 1776

w = {(4) + [(7 + 1) 26 / 10] + (76) + (76/ 4) + (17/ 4) – [2 (17)]} mod 7

Add the (7+1) to get 8 and multiply by 26 in the 2nd term and multiply 2 by 17 in the 6th term

w = {(4) + (208 / 10) + (76) + (76 / 4) + (17 / 4) – (34)} mod 7

Do the division in the 2nd, 4th, and 5th terms. The 2nd term is 20.8 keeping the 20, the 4th is 19 even, and the 5th is 4.25 keeping the 4

w = {(4) + (20) + (76) + (19) + (4) – (34)} mod 7

Add the five terms and subtract the 6th

w = {89} mod 7

Divide 89 by 7 and only keep the remainder. It’s 12 (7 x 12 = 84) with 5 left over.

w = 5

The week number w corresponds to the day of the week with Saturday = 0, Sunday = 1, Monday = 2, all the way up to Friday = 6. So w = 5 is then Thursday.

Check to see if this is correct here.

Why does this method work?  There are a number of explanations on the web - here's one.

No comments:

Post a Comment