Get Day of Week from datetime in Python – Number & Name (3 Examples)
In this Python programming tutorial, you’ll learn how to return the day of the week from a datetime object as a number or name.
Table of contents:
Let’s get started…
Example Data & Libraries
To be able to use the functions of the datetime module, we first need to import datetime:
import datetime # Import datetime module in Python
Furthermore, have a look at the example data below:
my_date = datetime.datetime(2021, 8, 3, 16, 48, 20) # Create Example date print(my_date) # Print example date # 2021-08-03 16:48:20
The previous output of the Python console will be used as the basic for the following two examples.
Example 1: Get Day of Week from datetime Applying weekday()
In the first example, I demonstrate how to return the weekday of a datetime object as a number by using the weekday function.
my_weekday_1 = my_date.weekday() # Using weekday function print(my_weekday_1) # Return weekday in number format # 1
As you can see by the output above, we get 1 as a result, since the days are indexed from Monday (0) to Sunday (6). So in other words, the weekday of our example date is Tuesday.
Example 2: Get Day of Week from datetime Using isoweekday()
Another opportunity to get the weekday in number format is the isoweekday function.
Consider the following code:
my_weekday_2 = my_date.isoweekday() # Applying isoweekday function print(my_weekday_2) # Print number of weekday # 2
As you can see, this time 2 is returned. The explanation is that the isoweekday function is based on another index (Monday = 1 to Sunday = 7).
Example 3: Get Day of Week from datetime as Name
If we want to get the weekday returned directly in name format, we can use the strftime function combined with the format code ‘%A’
Take a look at the following Python syntax and its output:
my_weekday_3 = my_date.strftime('%A') # Use strftime function print(my_weekday_3) # Return day as name # Tuesday
Video, Further Resources & Summary
Have a look at the following video on my YouTube channel. I demonstrate the Python code of this tutorial in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might want to read the related Python programming articles on this homepage. I have released several posts already.
- Get Week Number of datetime in Python (Example)
- Subtract Days, Months & Years from datetime Object in Python (3 Examples)
- Convert datetime Object to Date Only String in Python (Example)
- AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’ in Python (2 Examples)
- Python Programming Examples
You have learned in this article how to show the day of the week from datetime object as name or number in the Python programming language. If you have additional questions, let me know in the comments section.
This page was created in collaboration with Matthias Bäuerlen. Have a look at Matthias’ author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.