# What Day Of The Week

## What Day Of The Week

2.5. What day of the week was 2 August 1953?
--------------------------------------------

To calculate the day on which a particular date falls, the following
algorithm may be used (the divisions are integer divisions, in which
remainders are discarded; % means all we want is the remainder):

a = (14 - month) / 12
y = year - a
m = month + 12*a - 2
For Julian calendar: d = (5 + day + y + y/4 + (31*m)/12) % 7
For Gregorian calendar: d = (day + y + y/4 - y/100 + y/400 + (31*m)/12) % 7

The value of d is 0 for a Sunday, 1 for a Monday, 2 for a Tuesday, etc.

Then I sat down and came up with this formula in order to calculate dates such as "The third Monday in January". I suspect these have been derived and written down somewhere by someone else; in any case, these formula are easy to derive, and useful for computing various holidays in electronic calendars.
First, let the above formula be called DoW(year,month,dayinmonth), which specifies that its arguements are the year (in numerical form), the month (1-12) and the day in the month (day number in month, 1-31).

In all the below formula, the following common-sense relation is used: -1%7 = 6; -2%7=5; .. -6%7=1, -7%7=0. Also, an N-day is a Sunday (N=0), through Saturday (N=6). The most generic formula is then:

Date In Month that is an N-day ON OR AFTER date Year-Month-Day =
Day + (N - DoW(Year,Month,Day))%7 .

Date In Month that is an N-day ON OR BEFORE date Year-Month-Day =
Day - (DoW(Year,Month,Day) - N)%7 .

These lead to quick formulae for determining the date of the first, second, third, fourth and fifth occurence of a Sunday, Monday, etc., in any particular month:

First N-day: N1 = 1 + (N - DoW(Year,Month,1))%7 ;
2nd N-day : N2 = 8 + (N - DoW(Year,Month,8))%7 ;
3rd N-day : N3 = 15 + (N - DoW(Year,Month,15))%7 ;
4th N-day : N4 = 22 + (N - DoW(Year,Month,22))%7 ;
5th N-day : N5 = 29 + (N - DoW(Year,Month,29))%7 .
(Note: Use common sense when trying to calculate the fifth N-day: check to see if the value you obtain is greater than the number of days in the month; if it is, the there is no fifth N-day in that month.)

Two visitors to this page, Timothy Barmann and Bobby Cossum, have independently suggested that the above five equations can be simplified into just one equation. Let Q be the occurence (first, second, third, fourth, fifth), and N will still represent the day of the week, as above. Then,
the Q-th N-day: NQ = 1 + (Q-1)*7 + (N - DoW(Year,Month,1))%7;
or equivalently
the Q-th N-day: NQ = 7*Q - 6 + (N - DoW(Year,Month,1))%7. So, to find the first Friday using the above equations, use Q=1, N=5; the third Monday is found using Q=3, N=1, etc.

In order to find, for example, the LAST Monday in a month, we need to know the length of the month; for all months except February, this is, of course, fixed. In any case, we have:

ND=Number of last day in month;
Last N-Day : NL = ND - (DoW(Year,Month,ND) - N)%7 .
Example: What date is the last Monday in May, 1996?

The last day in May is May 31, so ND=31.
Monday is what we want, so N=1
The day of the week of May 31, 1996 is found by following the first algorithm above: a=(14-5)/12=0
y=1996-a=1996-0=1996
m=5+0-2=3
d=(31+1996+499-19+4+(31*3)/12)%7= 5
So, May 31st is a Friday; then
NL=31-(5-1)%7=31-4=27
So, the last Monday in 1996 May is May 27.

this is gay
fucked up on November 04, 2005 at 06:24
ok... I just use a calender...
Rita on November 07, 2005 at 12:13
this is fkd up
natalie on December 03, 2005 at 06:59
There is an error in there somewhere.
Jacob on December 06, 2005 at 08:51
toooooo many numbers
quita on December 10, 2005 at 11:39
toooooo many numbers
quita on December 10, 2005 at 11:39
Whaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaat???
Vicky on December 18, 2005 at 04:19
This is boring i didnt even read it. It makes the subject of condoms
interesting!
Secksy on January 18, 2006 at 07:49
sS something has to be wrong w/ you do you know how many kids get on
here
wow on February 06, 2006 at 07:13
ok.... I could do that........Or I could look it up...... or i could
not care because it is not important whether it was a monday or not in
1953......or i could kill all nerds!!!!! or a combination of the last
3 ;)
Uhhh on September 28, 2006 at 09:15
ok wat kind of a sdoopid person sits dre nd works out sumfink as
pointless as dat ?? nd i fort i had a borin lyf .. lyk wtf !!!
.x.tori.x. on November 11, 2006 at 10:51
Whoever figured out this algorithm, please post more. OK, maybe not
on this site... ;) Do human calculators use this sort of thing? I
always wondered. BTW, some ppl actually have brains. That they use.
On a daily basis. Oh, of course I'm not talking to anyone on this
page.
hmm on November 22, 2006 at 09:18
Does anybody actually care? This is jus shit that keeps nerds
entertained for hours!We can just look up a calendar!And anyway hoo
actually wants to kno if it was a monday or not on the 2 August 1953?
You should give up this crap! x
RonaxBabie on December 16, 2006 at 08:00
this is why we have CALENDARS!
coolio on December 18, 2006 at 02:42
Z on December 30, 2006 at 09:03
i hate maths and i jus look at the calnder !!!!tnkz the celander!!
maths hater on February 08, 2007 at 11:44
Too many numbers for a dumb person like me!
number hater on February 09, 2007 at 02:37
weird and to much for me to read and not going to. =:)
phone on February 09, 2007 at 02:39
dumb dumb dumb dumb dumb dumb dumb dumb dumb dumb dumb dumb dumb dumb
dumb dumb dumb dumb dumb...........you get the idea.
jesie on February 12, 2007 at 02:32
hurz hurz
teer on February 17, 2007 at 12:10
i didn't even read! who would waste their time on that anyway?!?
lalala on February 20, 2007 at 05:02
matt street is a fat shit and suck smal penis also that is fucked up a
bit like matts face wtf!
i fucked ur ma! on March 14, 2007 at 02:20
this is a true thing that happens! its not a chain letter! its kinda
scary at first but it really works!! paste this message into 3
comments and press ALT F4 and your crushes name will appear on the
screen!!! its soo wierd!
katherine on March 14, 2007 at 02:44
i didnt even read it and it still looked hard
darby on March 15, 2007 at 07:40
psht...easy. It's just algebra.
violet on March 23, 2007 at 06:24
yeah it's easy but why spend 15 minutes figuring it out when you can
use a calender?
person on March 28, 2007 at 01:51
I love math but this, this won't help us get anywhere in life, It
could help but the odds are against us.
Rafie on May 06, 2007 at 10:31
This is exactly the logic I was looking for in a project I'm on.
Thanks!
Zee on May 08, 2007 at 12:15
This is really confusing and way 2 many numbers
Maddy on May 18, 2007 at 06:27
i was too bored to read it
hello on June 01, 2007 at 07:15
??????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????
?????????????.................What?
????? on June 09, 2007 at 06:43
huhh??!!:D
hadeel_ayoubi on June 12, 2007 at 02:07
But u know wut?! u worked really hard and wut u did up there is
IMPRESSIVE!!I meanr u a genious or wut?! gd job!
bluh! on June 12, 2007 at 02:11
omfg whoeva made this up is just a math freak. just use a damn
calender its a lot quiker
nikki on June 16, 2007 at 05:50
I don't get a word it
said!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
huh!!!!!!11 on July 01, 2007 at 07:07
hill
8i m sooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo h on July 25, 2007 at 06:49
CCCCCCCOOOOOOOOOONNNNNNNNNNFFFUUUUUUUUUUSSSSSSSSSEEEEDDDDDDDDDD !!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!111 !!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!
!!!!!!!!!! !!!!!!!! !!!
sprite on August 15, 2007 at 08:35
It works when you treat all the variable as ints. (Ignoring any
remainders after every calculation)
Dustin on August 16, 2007 at 11:45
help! i used this formula in a perl
script: \$day=5; \$month=12; \$year=2007; \$a = (14 - \$month) /
12; \$y = \$year - \$a; \$m = \$month + 12*\$a - 2; #\$d = (5 + \$day + \$y
+ \$y/4 + (31*\$m)/12) % 7; \$d = (\$day + \$y + \$y/4 - \$y/100 + \$y/400 +
(31*\$m)/12) % 7; and i don't get the right answer. any ideas why
not? - g
lost guy on August 19, 2007 at 01:52
you ppl have no life! hahahahaahaha. why would you even waste your
oh my god on August 27, 2007 at 01:34
I just skimmed over it... I don't know why people even bother with
these solutions to problems. They're useless!
Kit Cat on September 30, 2007 at 12:39
WTF? I don't get it this is just retarded.
Yoshimura on October 13, 2007 at 02:42
u freaks who the fuk does this get a life !!! or buy a calender or
maybe go out on a d8 if u can...
sexgod on October 30, 2007 at 10:21
You get a life, you're wasting your time on a website you don't even
care for, and blogging. Because you don't have a life... you have
nothing better to do than sit at the computer. Sorry, gotta go.... I
have a life , you know.
Kit Cat Again on December 08, 2007 at 02:50
WTF is this crap, who cares. You have no life
Wtf on December 29, 2007 at 05:32
this is crap. its so confusing. just get a calender.
mmmeee on January 04, 2008 at 05:51
wested my time
HaHa on January 14, 2008 at 12:59
=NOT INTERESTED; zzzzzZZZZZZZZZZZZZZ,....... Let's just sleep
Deyzee Rae on January 26, 2008 at 09:52
Really great - I needed a simple formula to calculate the date for the
nightly backup - this works! Thanks!
Ron on March 22, 2008 at 03:36
I m a normal guy....I can't read it ... It's too hard n too BIG!
Jazzy on March 29, 2008 at 09:01
jesie, Jazzy, Deyzee Rae, Kit Cat, Yoshimura et al: My gods, you lot
are totally brainless CHAVS! Someone goes out of their way to put
something *useful* on the Web, and all you can say is 'this is gay',
'this is crap', or the ultimate red-necked neddy statement 'Get a
life'! Look at all the *other* people who thanked the poster for
helping them out with a problem they had, then FUCK OFF to some
for thoughtless entertainment.
ME on April 04, 2008 at 01:57
This is useful when you have to fill out an expense report from 3
years back...
Archee on April 17, 2008 at 01:37
Yes thats true. actually we discovered this for the first time.
Taliban on June 20, 2008 at 01:19
why do sum ppl only say, 'this is gay', or 'this is fkd up'?, for gods
sake, get a life, its only a quiz!
ricki on July 12, 2008 at 10:56
You get a life you fcking homo people say its gay because it fcking
IS!! you try and work out the fcking question yourself douche bag,
your probably like an 11 year old kid with no life and has no friends
cuz your a fcking computer nerd P.S- btch :-|
Gaber on July 23, 2008 at 06:42
that was pathatic it would be so much easier if dr.suess used a
calender
asia on July 24, 2008 at 03:05
fuck dick ballacks
g3org3 on December 18, 2008 at 01:03
this is so boring!!
Gema on February 08, 2009 at 11:15
this is gay
f u on July 26, 2011 at 08:35
calculate the day on which a particular date falls.
Ali khan on January 23, 2012 at 06:46