Algorithm to Calculate Day of Week for Any Given Date in Past or Future

By: Rohit Nirmal (www.RohitNirmal.com)


Century Table 

700–1799      4     (Still Julian Calendar in Great Britain and its colonies until 1752)    
1800–1899     2
1900–1999     0
2000–2099     6
2100–2199     4
2200–2299     2
2300–2399     0
2400–2499     6
2500–2599     4
2600–2699     2


Months Table
January      0 (in leap year 6)
February     3 (in leap year 2)
March        3
April        6
May          1
June         4
July         6
August       2
September    5
October      0
November     3
December     5

Days Table
Sunday    0
Monday    1
Tuesday   2
Wednesday 3
Thursday  4
Friday    5
Saturday  6



Example
-------

Find day of week for 1 August 2010

   1. Look up the 2010 in the centuries table: 6
   2. Note the last two digits of the year: 10
   3. Divide the 10 by 4: 10/4 = 2.5 and drop the fractional part: 2
   4. Look up August in the months table: 2
   5. Add all numbers from steps 1–4 to the day of the month (in this case, 1): 6+10+2+2+1=21.
   6. Divide the sum from step 5 by 7 and find the remainder: 21/7= 3 remainder 0
   7. Find the remainder in the days table: 0 = SUNDAY 

Answer is SUNDAY 

Let's try leap year January 1, 2000

   1. Look up the 2000s in the centuries table: 6
   2. Note the last two digits of the year: 00
   3. Divide the 00 by 4: 0/4 = 0 and drop the fractional part: 0
   4. Look up January in the months table: 6 (leap)
   5. Add all numbers from steps 1–4 to the day of the month (in this case, 1): 6+00+0+6+1=13.
   6. Divide the sum from step 5 by 7 and find the remainder: 13/7=1 remainder 6
   7. Find the remainder in the days table: 6=Saturday.


Now let's use April 24, 1982.

   1. Look up the 1900s in the centuries table: 0
   2. Note the last two digits of the year: 82
   3. Divide the 82 by 4: 82/4 = 20.5 and drop the fractional part: 20
   4. Look up April in the months table: 6
   5. Add all numbers from steps 1–4 to the day of the month (in this case, 24): 0+82+20+6+24=132.
   6. Divide the sum from step 5 by 7 and find the remainder: 132/7=18 remainder 6
   7. Find the remainder in the days table: 6=Saturday.