Return Actual Day of Week in Python – Name and Number (3 Examples)
In this post, you’ll learn how to print the actual weekday in number or name in the Python programming language.
Setting up the Examples
We can load the datetime module as shown here:
from datetime import datetime # Import library to Python |
from datetime import datetime # Import library to Python
The data below will be used for the examples of this article.
x = datetime.now() print(x) # 2022-06-10 13:48:30.861328 |
x = datetime.now() print(x) # 2022-06-10 13:48:30.861328
Example 1: Get Name of Actual Day of Week
To show the name of the current day of the week, consider the following python code.
my_day_of_week_1 = x.strftime('%A') # function strftime() print(my_day_of_week_1) # Print name of weekday # Friday |
my_day_of_week_1 = x.strftime('%A') # function strftime() print(my_day_of_week_1) # Print name of weekday # Friday
Example 2: Return actual Day of Week with isoweekday function
To get the actual day of the week as an integer, we can use the isoweekday function.
my_day_of_week_2 = x.isoweekday() # function isoweekday() print(my_day_of_week_2) # Return number of weekday # 5 |
my_day_of_week_2 = x.isoweekday() # function isoweekday() print(my_day_of_week_2) # Return number of weekday # 5
Based on the index (1 to 7 = Monday to Sunday) our output is 5.
Example 3: Return Day of Week Applying weekday function
Alternatively to the isoweekday function, we can also use the weekday function:
my_day_of_week_3 = x.weekday() # function weekday() print(my_day_of_week_3) # Show number of weekday # 4 |
my_day_of_week_3 = x.weekday() # function weekday() print(my_day_of_week_3) # Show number of weekday # 4
This time, the output is 4 because of the different index count (Monday is 0 and Sunday is 6).
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.