How to determine the day of the week 📅

MSDS 610 - Biz Comm

October 1st, 2018

What day of the week were you born? 🎂

Dates are hard

Dates are hard

Dates are hard

Today we are going to learn how to impress family and friends 🤓

General idea 🛠️


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


• Modular arithmetic.

What do we know about the calendar? 🤔

• A common year has 365 days.

• A leap year has 366 days.

• 365 mod 7 = 1.

January 1st, 2018 = Monday

$\Rightarrow$

January 1st, 2019 = Tuesday

What else do we know about the calendar? 🤔

• In a common year, February has 28 days.

• 28 mod 7 = 0.

• Thus, in a common year, February and March start on the same day.

• Bad news 😫 - Not as easy for the other months!

• Good news 😄 - Some months do start the same day as others.

• For example:

- September has 30 days.
- October has 31 days.
- November has 30 days.

- 30 + 31 + 30 = 91.
- 91 mod 7 = 0.

• Therefore, September and December start on the same day! (Always!)

• Corresponding months in a common year:

• Corresponding months in a leap year:

The algorithm 🤓


In [1]:
# Target date
year = 2018
month = 10
day = 1

Step #1

Get the number for the corresponding century from table:

In [2]:
def get_century(year):
    if (1700 <= year and year <= 1799):
        return 5
    
    if (1800 <= year and year <= 1899):
        return 3
    
    if (1900 <= year and year <= 1999):
        return 1

    if (2000 <= year and year <= 2099):
        return 0
    
    if (2100 <= year and year <= 2199):
        return -2
    
    if (2100 <= year and year <= 2199):
        return -4
In [3]:
x1 = get_century(year)
x1
Out[3]:
0

Step #2

Get the last two digits from the year:

In [4]:
def get_two_digits_year(year):
    return year%100
In [5]:
x2 = get_two_digits_year(year)
x2
Out[5]:
18

Step #3

Divide the number from step two by 4 and get it without the remainder:

In [6]:
def get_no_remainder(x2):
    return int(x2/4)
In [7]:
x3 = get_no_remainder(x2)
x3
Out[7]:
4

Step #4

Get the month coefficient:

In [8]:
def get_month(month):
    if month == 1 or month == 10:
        return 6
    
    if month == 2 or month == 3 or month == 11:
        return 2
    
    if month == 4 or month == 7:
        return 5
    
    if month == 5:
        return 0
    
    if month == 6:
        return 3
    
    if month == 8:
        return 1
    
    if month == 9 or month == 12:
        return 4
In [9]:
x4 = get_month(month)
x4
Out[9]:
6

Step #5

If the year is leap and the month is January or February, return -1. Else, return 0.

In [10]:
def is_leap_year(year):
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

def leap_coefficient(year,month):
    if is_leap_year(year) and (month==1 or month==2):
        return -1
    else:
        return 0
In [11]:
x5 = leap_coefficient(year,month)
x5
Out[11]:
0

Step #6

Get the day:

In [12]:
x6 = day
x6
Out[12]:
1

Step #7

Sum the numbers from steps 1 to 6, divide by 7, and take the remainder:

In [13]:
def return_remainder(year,month,day):
    x1 = get_century(year)
    x2 = get_two_digits_year(year)
    x3 = get_no_remainder(x2)
    x4 = get_month(month)
    x5 = leap_coefficient(year,month)
    x6 = day
    total = x1 + x2 + x3 + x4 + x5 + x6
    return total%7
In [14]:
return_remainder(year,month,day)
Out[14]:
1

Step #7

Actually find out the day of the week!

In [15]:
def get_day_of_week(year,month,day):
    remainder = return_remainder(year,month,day)
    days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    return f"{year}-{month}-{day} is a {days[remainder]} 🎉"
In [16]:
get_day_of_week(year,month,day)
Out[16]:
'2018-10-1 is a Monday 🎉'
In [17]:
from datetime import date
import calendar
my_date = date(2018, 10, 1)
calendar.day_name[my_date.weekday()]
Out[17]:
'Monday'

But how are you going to impress your friends and family? 🤫🤫🤫

Recap ✍️

• Step 1: Get century coefficient. (Tip: 1900's = 1, 2000's = 0) $\Rightarrow$ 0

• Step 2: Get last two digits of the year. $\Rightarrow$ 18
• Step 3: Divide step two by 4. $\Rightarrow$ 4

• Step 4: Get corresponding month coefficient. $\Rightarrow$ 6

• Step 5: If leap and (JAN or FEB) = -1. $\Rightarrow$ 0

• Step 6: Get day. $\Rightarrow$ 1

• Step 7: Total mod 7. $\Rightarrow$ 0+18+4+6+0+1 = 29 mod 7 = 1

One more 👀

In [18]:
year = 2022
month = 2
day = 22
In [19]:
get_day_of_week(year,month,day)
Out[19]:
'2022-2-22 is a Tuesday 🎉'

One more 👀

In [20]:
year = 2022
month = 2
day = 22
In [21]:
get_day_of_week(year,month,day)
Out[21]:
'2022-2-22 is a Tuesday 🎉'

Thank you for your attention 🤗




github.com/vivianamarquez/Determine-Day-of-the-Week