Return Weekday from datetime Object in Python – Name or Number (3 Examples)

In this article, I’ll show how to print the weekday from a datetime object as name and number in Python.

Setting up the Examples

Before we can start, we have to load the datetime module to Python:

import datetime                                  # Load datetime module

We use the created datetime object below for the following examples:

x = datetime.datetime(2023, 5, 7, 11, 32, 15)    # Example date
print(x)                                         # Return example date
# 2023-05-07 11:32:15

Example 1: Return Day of Week from datetime as Name

To get the name of the weekday, we apply the strftime function, including the ‘%A’ parameter:

my_day_of_week_1 = x.strftime('%A')
# Sunday

Example 2: Get Day of Week from datetime as Number

If we want to get the weekday as integer, we can use the isoweekday function:

my_day_of_week_2 = x.isoweekday()                # function isoweekday()
print(my_day_of_week_2)                          # Number of weekday
# 7

The output is 7, because the isoweekday function counts from Monday (1) to Sunday (7).

Example 3: Return Day of Week Applying weekday function

Another option for getting the weekday of a datetime object as number is the weekday function:

my_day_of_week_3 = x.weekday()                   # weekday() function
print(my_day_of_week_3)                          # Number of weekday
# 6

This time, the output is 6 because the index counts from 0 (Monday) to 6 (Sunday).

Further Resources

Here, you can find some additional resources on topics such as groups, numeric values, and data objects.


Matthias Bäuerlen Python Programmer

Note: This article was created in collaboration with Matthias Bäuerlen. Matthias is a programmer who helps to create tutorials on the Python programming language. You might find more info about Matthias and his other articles on his profile page.

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.
You need to agree with the terms to proceed