Posted by Marta on February 1, 2023 Viewed 41767 times
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.
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 week_days=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] week_num=datetime.date(2020,7,24).weekday() print(week_days[week_num])
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)
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 week_days=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] weekday= calendar.weekday(2020,7,24) print(week_days[weekday])
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): if(month<3): month+=12 year-=1 result = (day + (int((13*(month-2))/5))+year+(int(year/4))-(int(year/100))-(int(year/400)))%7 return int(result)+1 ##################### week_days=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] week_num = day_of_week(2020,7,24) print(week_days[week_num])
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!
Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications
Unique lay-out and teaching programming style helping new concepts stick in your memory
Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples
Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.
Excellent read for anyone who already know how to program and want to learn Best Practices
Perfect book for anyone transitioning into the mid/mid-senior developer level
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