How to Get the day of the week in Python

In this tutorial I will share how you can get the week day: Monday, Tuesday , etc given a certain date in Python. Using libraries and without libraries.

Card image cap

This article will explain how you can get the day of the week: Monday, Tuesday, etc., given a specific date using Python. Here are some example of inputs and outputs:

Input: 2020, 7, 24
Output: Friday  

I will share different approaches to solve this problem using various libraries.

Get the day of the week using the datetime library

The datetime library is a built-in python library, no need to install the library with pip. The datetime library is a library that helps to manipulate dates and times in both simple and complex ways. 

This library provides the method .weekday(), which returns a number between 0 and 7, representing the week’s day.

The code below gets the day of the week for a given day. This code will convert the date, formed by year, month, and day, into a datetime date. Then call the .weekday() method to get the ‘day of the week’ and map it to a specific day in text.

import datetime


Using datetime.strftime method

The datetime library provides another method that allows you to get the day of the week. The strftime() method includes functionality to create a string representing the given date. You can pass different format codes, and it will return different string representations of your date.




Other format code examples: ( See format code list here)




Using calendar library

The Calendar library is another built-in python library handy when it comes to date manipulation. Check out all functionality available

Why are there two libraries? Although some of the functionality overlaps, the calendar library provides methods that allow you to be iterative over the days of months. Or change the calendar, so the first day of the week is Sunday. In other words, extra functionality to manipulate the calendar.

And it also includes the method .weekday() to get a day of the week given a date formed by year, month and day. Here is an example of how to use this method.

import calendar

weekday= calendar.weekday(2020,7,24)



Without using libraries

You could also avoid using libraries and implement this functionality yourself. To do so, first, I will advise you to take a look at the Zeller’s formula for the Gregorian calendar. This formula will help you to get the day of the week, the number from 0 to 7, based on the Gregorian calendar. 

The code below is implementing Zeller’s formula. This code is only considering the Gregorian calendar, which is what most of the world uses nowadays.

def day_of_week(year,month,day):
    result = (day + (int((13*(month-2))/5))+year+(int(year/4))-(int(year/100))-(int(year/400)))%7
    return int(result)+1

week_num = day_of_week(2020,7,24)




To summarise, this tutorial covered different ways to get the day of the week in Python. You could use the datetime or the calendar library, both built-in libraries. Or you could implement the functionality yourself using Zeller’s formula.

I hope this tutorial was helpful. Thank you so much for reading and supporting this blog! Happy Coding!

Recommended articles

Learn Ethical Hacking From Scratch Course

Become an ethical hacker that can hack computer systems like black hat hackers and secure them like security experts.

Project-Based Programming Introduction

Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications

100% Recommended book for Java Beginners

Unique lay-out and teaching programming style helping new concepts stick in your memory

90 Specific Ways to Write Better Python

Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples

Grow Your Java skills as a developer

Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.

Write Code as a Professional Developer

Excellent read for anyone who already know how to program and want to learn Best Practices

Every Developer should read this

Perfect book for anyone transitioning into the mid/mid-senior developer level

Great preparation for interviews

Great book and probably the best way to practice for interview. Some really good information on how to perform an interview. Code Example in Java