How to calculate the day of the week of any date

How to Calculate the Day of the Week of Any Date
How can you quickly and accurately calculate the day of the week associated with a specific date? The Doomsday Algorithm. Credit: Getty Images

Martin Gardner is the inspiration behind this column. As many readers know, Gardner wrote his own column, called “Mathematical Games”, in Scientific American from 1956 to 1985.

It sat squarely within the nexus where recreational mathematics, puzzles, games, magic, art and science intersect. His compiled articles required 14 large volumes to contain them all.

Those, and the several dozen other books he wrote in his 95 years, comprise an incredible body of work. Every time I sit down to write, I ask myself if Martin would have been interested in the subject matter.

Recently I attended the Gathering 4 Gardner in Atlanta, Georgia. The G4G, as it’s called by attendees, is a biennial conference of 300 mathematicians, puzzle makers, magicians and artists who consider themselves ardent Martin Gardner fans.

They gather in even-numbered years to celebrate and share the latest and greatest discoveries in recreational mathematics. Gardner himself was present at the first two conferences in the early 1990s, but was uncomfortable with the celebrity attention he received.

He gave his blessing for future events but never attended again.

This year’s final dinner show closed with a demonstration by mathematician and acknowledged genius John Horton Conway. Conway is Professor Emeritus of Mathematics at Princeton University, with expertise in knot theory, number theory and combinatorial game theory. He is perhaps most famous for creating the computer simulation called Game of Life, in 1970.

Conway asked three audience members to stand. Each was asked to name their birthdate. In turn, Conway, who is nearly 80, almost instantly announced the day of the week on which they were born. The room erupted in applause.

What he did is commonly referred to among magicians and recreational mathematicians as “calendar calculation”. Conway uses a system he devised in the early 1970s (curiously, after a conversation with Martin Gardner) called the Doomsday Algorithm. The process involves memorising codes, century-specific days, and dividing certain numbers by 12 or four.

There is a similar but simpler method to determine the day of the week for any date. If you can do some basic division and addition in your head, you should, with practice, be able to perform the calculation in a matter of seconds.

First, you need to memorise the following “month codes”:

“Month codes” for the months of the year. Credit: Cosmos Magazine

In leap years, subtract one from the month code for January and February only! Leap years are any years where the last two digits of the year are a multiple of four. The  exception to this rule are century years (those ending with 00) where the whole number must divisible by 400. Thus 1800 and 1900 were not leap years, while 1600 and 2000 were.

  • To begin, you take the last two digits of the year and divide the number by four.
  • Disregarding any remainder, add the result to the number you began with.
  • Add the month code number.
  • Add the day of the month.
  • Divide this total by seven.

Now disregard the whole number and focus on the remainder. It is the remainder that will tell you the day of the week, according to the following day codes.

“Day codes” for the days of the week. Credit: Cosmos Magazine

They are relatively easy to remember since they begin with Sunday as the first day of the week. The only quirk is that a remainder of zero equates to Saturday.

For dates in the 1800s, add two to your total. For dates in the 2000s, subtract one. You can do this any time before the final step of dividing by seven.

Let’s look at an example: 17 January 1953.  The first step is to divide 53 by four, ignoring any remainders. Answer: 13.

Add 13 to 53, to get 66.  Add the “month code” (which for January is one) to get 67. Add the day of the month (17) to 67, making the result 84.

Finally the last step; divide 84 by seven. The answer is 12 with a remainder of zero. All we care about now is the remainder. Zero tells us that 17 January 1953 was a Saturday.

The mental division steps are the most difficult part of this method. Credit: Cosmos Magazine

The mental division steps are the most difficult part of this method so here’s a tip: you can “cast out sevens” as you go! 

For example, take the date 6 December 1920. 20 divided by four is five. Adding five back to 20 gives us 25. If you want, you can divide 25 by seven right now and remember only the remainder (which is four).

Add this remainder to the month code for December (six) to get 10. Divide by seven again and keep the remainder (which is now three). Add this new remainder to the day you want (the sixth) to get nine. Divide by seven one final time to get a remainder of two.

This tells us that 6 December 1920 was a Monday.

Casting out or dividing by seven as you go at each step is usually much easier than initially doing all of the addition and then dividing the large total by seven at the very end. The result is the same either way.

With practice just about anyone can get this method down to under 15 seconds. With a lot of practice, you might just become the next John Conway!

How to Calculate the Day of the Week of Any Date

There’s never been a more important time to explain the facts, cherish evidence-based knowledge and to showcase the latest scientific, technological and engineering breakthroughs.

Cosmos is published by The Royal Institution of Australia, a charity dedicated to connecting people with the world of science.

Financial contributions, however big or small, help us provide access to trusted science information at a time when the world needs it most. Please support us by making a donation or purchasing a subscription today.

Make a donation How to Calculate the Day of the Week of Any Date

Calculate the day of week of any date… in your head

If you want to really impress people at cocktail parties — and by
impress I mean freak them out with your uncanny, weird, rainman-like borderline
Asperger's-syndrome abilities — compute the day of week of any date
in your head. With a little bit of memorization and some basic computational
arithmetic skill, you can figure out that, for example, Jul. 4 1776 fell on
a Thursday, or that your friend's birthday will fall on a Sunday three years
from now.

See also:  How to think about astronomically large numbers

I started working this out when my kids were little, as I was driving with
them cross-country — my son kept asking me on what day of the week he
was born. I knew the date of his birth, but I couldn't for the life of me
remember what day it actually fell on.

I remembered that it happend during
the week, since I had to leave work when my wife told me she had gone into
labor, but that's all I could come up with.

Since I was in the car, with no
(convenient) access to a calendar — and nothing particularly better to
do with myself — I started trying to work it out in my head.

Although I didn't figure it out on that car trip, I worked out the basic
algorithm that I'll present here, since it makes a sort of an interesting
computational problem as well. Conceptually, it's a simple problem:

  1. Figure out how many days occurred between the target date and the current date.
  2. Divide this by 7 and compute the remainder R
  3. If the target date is in the past, count backwards from the current day of the week by R days; if in the future, count forwards.

Of course, this glosses over the computationally tricky problem of figuring
out how many days passed between the target date and the current date. If
you're going to compute this without pencil and paper, you need to simplify
the problem a bit.

The first key observation is that there are
52 weeks in a year, but 365 days in a (non-leap) year. Since 52 * 7 = 364,
that means that there's one “extra” day in each year. So, if today is
Thursday, Mar. 15, 2012, then Mar. 15, 2013 will fall on a Friday. Mar.

15,
2014 will fall on a Saturday. Mar. 15, 2015 will fall on a Sunday, and so on.

So, if you want to figure out what day a given date fell on (or falls on), first figure out what day that date will fall on this year, and then
add or subtract one day for each intervening year.

Leap years make this a bit more complicated, since you have to add
one day for each intervening leap year… but that's not impossible to work
out in your head, either. Just add an extra day
for each multiple of four that occurs between the current year and the target
year.

For instance, today is Monday, Jan. 9, 2012. What day did Jan. 9, 1941 fall
on? Well, for starters, 2012 – 1941 = 71; you can figure this out in your
head easily. How many leap years occured in the intervening 71 years? Well,
71 / 4 = 17, so there were 17 leap years in between. Now,
add that 17 to 71 to get 88.

Now it's a simple
matter of figuring out the remainder of 88 / 7 to figure out how many days
of the week to “subtract” from the current one to figure out what day Jan. 9,
1941 fell on. Dividing by 7, you get a remainder of 4 – so subtract four
days from the current day of Monday to determine that Jan.

9, 1941 fell on
a Thursday.

One thing to be aware of if you're computing dates way into the future
or way in the past; years that are multiples of 100 are not leap years, even though they're evenly divisible by 4… unless they
are divisible by 400.

So you have to again subtract 1 for each multiple of 100 that occurs between the source and target year, excluding the multiples of
400: 2400, 1600, 1200, …

This won't be difficult unless you're trying to
compute back to the dark ages, in which case you should probably be aware
that the current leap year rules didn't go into effect until 1582. If you
need to compute that far back, just get a calendar.

So, algorithmically speaking, the offset between the same day on different
years is given by:

[ ( target year – source year ) + ( target year – source year ) / 4 ) ] % 7

One last adjustment: if the target or source year is a leap year
— is evenly divisble by 4 — you have to add one day if the
earlier date falls in January or February, and add one day if the later
date falls after February.

Obviously, you'll only ever have to adjust one
in this case, so it's not too difficult to keep track of this. It's also easy
to tell if a date falls on a leap year – if the last two digits are evenly
divisible by 4, then it's a leap year.

Since 76 is evenly divisible by 4,
for instance, 1776 was a leap year (and so was 1876, 1976, and so will be
2076, etc.).

Now, if you want to extend that out to any arbitrary date, first figure out
what day your chosen date will fall on in this year, and then perform
the computation to figure out the difference.

Unfortunately, figuring out
what day a given date falls on in the current year is a bit harder than
figuring out the difference between two dates in different years.

It would
be trivial if each month had exactly 30 days; then you could just compute
the difference in months and multiply by 2 (since 30 % 7 = 2). The actual
differences are given by the table below.

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Jan 3 3 6 1 4 6 2 5 3 5
Feb 3 3 5 1 3 6 2 4 2
Mar 3 3 5 1 3 6 2 4 2
Apr 6 3 3 2 5 3 6 1 4 6
May 1 5 5 2 3 5 1 4 6 2 4
Jun 4 1 1 5 3 2 5 1 3 6 1
Jul 6 3 3 5 2 3 6 1 4 6
Aug 2 6 6 3 1 5 3 3 5 1 3
Sep 5 2 2 6 4 1 6 3 2 5
Oct 4 4 1 6 3 1 5 2 3 5
Nov 3 4 2 6 4 1 5 3 2
Dec 5 2 2 6 4 1 6 3 5 2

Table 1: Day offsets for all months

If you can memorize that behemoth, you don't need my help computing the
day that a given date falls on… is there a way to simplify this calculation?

See also:  What is gluten?

Well, actually, it's easier to just simplify the problem. One thing that
makes this computation difficult is that I'm trying to compute the number
of days between today and the target date.

But I don't actually need to do
that – all I'm really interested in is the day that the target date falls on.

If I can remember what day January 1st falls on in this year (Sunday, in 2012),
then I can just figure the offset between that day and the
date that I'm interested in. Now I only have to remember the very first row
of table 1.

Still – that's 12 pretty randomly distributed numbers… not easy to memorize.

Remember earlier when I mentioned that this would be easy if every month
had 30 days? Then I could just multiply the ordinal of the target month by
2 and that would give me my offset, mod 7.

Although months have varying numbers
of days, it's much easier to memorize how wrong this calculation is than
it is to memorize the actual differences themselves.

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 -1 1 1 2 3 3 4 4

Table 2: differences between various months.

Now, given a date in the current year, multiply its (zero-based) month ordinal
by 2 and then add the
number in table 2, which is easy to memorize. This gives you the “month offset” – and since your reference date is Jan.

1, the day offset is just the
day of the month. Add these two numbers, compute the remainder modulo 7, and
you've got your offset to figure out what day a given date falls in in the
current year.

Don't forget to account for leap year if the current year is
one, too.

So let's say you want to figure out what day Sep. 25, 2012 falls on, knowing
that Jan. 1 2012 falls on a Sunday. 8 * 2 = 16 + 3 (from table 2) = 19 + 1 since 2012 is a leap year = 20, which leaves a remainder of 6 modulo 7. Therefore,
Sep. 1 of 2012 must fall on a Saturday.

The 25th is 24 days after that; 24 leaves a remainder of 3 mod 7, so Sep. 25 of 2012 must then fall on a Tuesday.
Just don't forget to subtract 1 from the month and day to account for the fact
that all of these computation are “zero-based”.

If you're a C or Java
programmer, you shouldn't have any trouble keeping track of that.

In fact, it's not even necessary to keep track of what day January 1st falls
on in the current year – all you need is a known reference date, and you
can compute everything relative to that date, accounting for the year as
shown above. I just remember that Jan. 1, 2000 was a Saturday, and I can
compute everything else forward from there. Using the year 2000 as a reference
year makes my year calculations easy to keep track of, since it's easy to
subtract from.

So, let's put all of this together. What day will Jul. 4, 2019 fall on?
M = 6, D = 3, Y = 19. The year offset is 19 + ( 19 / 4 ) = 23 % 7 = 2, which
you can figure out in your head easily.

Add one since the “source year” of 2000 was a leap year to get 3. The month offset is (6*2)+1=13%7=6 and the day offset
is, of course, D=3. 6+3+3=12; 12%7=4, so Jul. 4 2019 is five days more than
the reference date of Saturday, Jan.

1, 2000: Thursday, Jul. 4 2019.

Here it is in code:

static int adjustment[] = { 0, 1, -1, 0, 0, 1, 1, 2, 3, 3, 4, 4 };

static int day_offset( int y, int m, int d )
{
int year_offset, month_offset, day_offset;

// Add 1 if y > 2000 to account for the fact that 2000 was a leap year.
year_offset = ( y – 2000 ) + ( ( y – 2000 ) / 4 ) + ( y > 2000 );
// Account for wrongly computed leap years
year_offset -= ( y – 2000 ) / 100;

// Add back 1 year if the target year is a leap year but the target
// day is after the 29th (in other words, the leap day hasn't happened
// yet).
if ( y % 100 )
{
year_offset += ( y < 2000 ) && !( y % 4 ) && ( m > 2 );
year_offset -= ( y > 2000 ) && !( y % 4 ) && ( m < 3 ); } month_offset = ( ( m - 1 ) * 2 ) + adjustment[ m - 1 ]; day_offset = d - 1; return ( year_offset + month_offset + day_offset ) % 7; } This isn't necessarily the most efficient way to do this on a computer, but encapsulates an algorithm that, with a bit of practice, you can memorize and repeat in your head next time you need to know if Christmas will fall on a weekend this year or not.

Add a comment:

Completely off-topic or spam comments will be removed at the discretion of the moderator.

You may preserve formatting (e.g. a code sample) by indenting with four spaces preceding the formatted line(s)

Day calculation from date

Note: This is not a java specific post. The below mentioned methods are not specific to any technology and can be implemented in any programming language.

INTRODUCTION

There are two formulas for calculating the day of the week for a given date.

  • Zeller’s Rule
  • Key-Value Method

Note: Both the methods work only for the Gregorian calendar. (People in English-speaking countries used a different calendar before September 14, 1752.)

1) Zeller’s Rule

F=k+ [(13*m-1)/5] +D+ [D/4] +[C/4]-2*C where

k is  the day of the month.
m is the month number.
D is the last two digits of the year.
C is the first two digits of the year.

Note:
According to Zeller’s rule the month is counted as follows:
 March is 1, April is 2….. January is 11 and February is 12.
So the year starts from March and ends with February.

So if the given date has month as January or February subtract 1 from the year. For example:
For 1st January 1998 subtract 1 from 1998 i.e. 1998-1=1997 and use 1997 for calculating D.


Discard all the decimal values and then find the final value of F.

After getting the value of F, divide it by 7.The value of F can be either positive or negative. If it is negative, let us suppose F = -15. When we divide by 7 we have to find the greatest multiple of 7 less than -15, so the remainder will be positive (or zero). -21 is the greatest multiple of 7 less than -15, so the remainder is 6 since -21 + 6 = -15.

Alternatively, we can say that -7 goes into -15 twice, making -14 and leaving a remainder of -1.If we add 7 since the remainder is negative i.e. -1 + 7 we again get 6 as remainder. After getting the remainder we can find the day of the week for the given date. Following are the values for the corresponding remainders:

Sun Mon Tue Wed Thurs Fri Sat
1 2 3 4 5 6
See also:  ‘council’ or ‘counsel’?

Examples for day calculation using Zeller’s Rule:

Let us calculate the day for the following dates:
1st April 1983 and 27th February 2023.
A) 1st April 1983:
Here
k = 1
m=2
D=83

C=19.

Putting the values in the formula, we get,
F= 1+ [(13*2-1)/5] +83+83/4+19/4-2*19
= 1+ [(26-1)/5]+83+20.75+4.75-38
= 1+25+83+20+4-38                 (discarding the decimal values)
= 133-38
= 75

  • After calculating F divide it by 7 and get the remainder.
  • 78/7=11 Quotient
    5-Remainder
  • Therefore, the day on 1st April 1983 was Friday since the remainder is 5.
  • B) 2nd March 2004:
    Here,
    k = 2
    m= 1
    D= 04
    C= 20.
  • Putting the values in the formula, we get,
  • F= 2+ [(13*1-1)/5] +04+04/4+20/4-2*20
    = 2+ [(13-1)/5] +04+01+05-40
    = 2+ [12/5] +10-40
    = 2+2+10-40                  (discarding the decimal values)
    = 14-40
    = -26

Here F is negative. So when we divide by 7 we have to find the greatest multiple of 7 less than -26, so the remainder will be positive (or zero). -28 is the greatest multiple of 7 less than -26, so the remainder is 2 since -28 + 2 = -26.

  1. So, the remainder is 2.
  2. Therefore, the day on 2nd March 2004 was Tuesday since the remainder is 5.
  3. C) 27th February 2023:
  4. Here,
  5. k = 27
    m = 12
    D = 22   (Since month count starts from March)
  6. C = 20
  7. Putting the values in the formula, we get,

F = 27+ [(13*12-1)/5] +22+22/4+20/4-2*20
= 27+ [(159-1)/5] +22+5.5+5-40
= 27+ [158/5] +22+5.5+5-40
= 27+ [31.6] + 22 + 5.5 + 5 – 40
= 27+ 31+22+5+5-40     (discarding the decimal values)
= 90-40
= 50

After dividing F by 7, we get remainder as 50/7=1.

Therefore, the day on 27th February 2023 is Monday since the remainder is 1.

2) The Key Value Method

The Key Value method uses codes for different months and years to calculate the day of the week. It would be easier if one is able to memorize the codes which are very easy to learn.

Steps:

  1. Take the last 2 digits of the year.
  2. Divide it by 4 and discard any remainder.
  3. Add the day of the month to the value obtained in step 2.
  4. Add the month’s key value, from the following table to the value obtained in step 3.
Jan Feb Mar Apr May June July Aug Sept Oct Nov Dec
1 4 4 2 5 3 6 1 4 6
  1. If the date is in January or February of a leap year, subtract 1 from step 4.
  2. Add the year (century) code from the following table.
1700s 1800s 1900s 2000s
4 2 6

Suppose the year is not in the above table.  In this case all we have to do is add or subtract 400 until we have a year (century) that is in the table. Then get the code for the year from the above table and add the value to the previous step (our running total).

  1. Add the last two digits of the year to the value we obtained in the previous step.

How to Calculate the Day of the Week for Any Date (Until 2099)

Have you ever wondered what day of the week your birthday will fall on next year, or tried to make holiday plans without knowing whether Christmas will fall on a weekday or weekend? There are some simple math tricks you can use to figure it out.

First, if you’re near a computer, don’t make your life more complicated than it needs to be. Plug the date into Google and it will spit out the day of the week.

SEE ALSO: Use This Math Trick to Be Smart With Your Money

But in a Mind Your Decisions video, Presh Talwalkar explains how you can do it mentally using something called the Doomsday rule. Particular dates throughout the year (known as Doomsday dates) always fall on the same day of the week as each other — a date we can calculate for any year until 2099 — and you can use that knowledge to figure out when other dates will take place each year.

  • Some of these dates are:
  • December 12 (12/12)
  • November 7 (11/7)

The first set is easy to remember because they are pairs of even numbers. Talwalkar suggests that you remember the second set using the mnemonic “I work 9 to 5 at the 7-11.”

A few more are: Pi Day (3/14), the last day of February (2/28 or 2/29) and January 3 (1/3) or January 4 (1/4) if it’s a leap year.

So, say we want to know when Christmas Eve of 2020 will be, if we know that Doomsday in 2020 falls on a Saturday (we’ll see how to calculate that in a moment), we can look for the closest date on our list to December 24 — December 12 — and calculate from there. December 26 will be a Saturday since it is two weeks from December 12, so Christmas Eve must be a Thursday.

But how can we know when Doomsday will be in the first place? All of the other calculations depend on that.

There’s a formula that we can use that calculates a one-digit code that represents a day of the week for New Year’s Day, with Sunday starting at 0 and Saturday ending at 6.

Code = (-1 + YY +⎡YY/4⎤) mod 7

If you’re not familiar with all of the symbols in that formula, that’s ok. They’re actually pretty simple. The weird brackets “⎡⎤” just mean “rounded up” and the “mod 7” stands for modulo 7 and means “divide by 7 and keep just the remainder.”

Let’s try it out for 2021:

Code = (-1 + 21 +⎡21/4⎤) mod 7

Code = 5

So New Year's day falls on a Friday. Doomsday will be January 3 since it isn’t a leap year, so we just have to count forward two days from Friday, which gives us Sunday.

Now it’s time to impress your friends! Not only will this work going forward until 2099, but you can also use it backwards to 2001.

Read next: How to Fairly Cut a Cake, According to Math

Be the first to comment

Leave a Reply

Your email address will not be published.


*