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.



Furthermore, you might want to read the related Python programming articles on this homepage. I have released several posts already.


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.


Matthias Bäuerlen Python Programmer

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.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.