Get Current Day of Week in Python – Number & Name (3 Examples)

 

In this article, I’ll demonstrate how to show the actual day of the week by number or name in Python.

The tutorial will contain three examples for the printing of the current weekday in name or number. To be more precise, the page is structured as follows:

Let’s get started.

 

Exemplifying Data & Libraries

If we want to use the functions of the library, we first need to load:

from datetime import datetime            # Import datetime module

We’ll use the following data as a basis for this Python tutorial.

my_date = datetime.today()               # Creating example data
print(my_date)                           # Print actual date
# 2022-06-10 13:49:00.507508

Have a look at the previous output of the Python console. It shows the actual date as datetime object.

 

Example 1: Get Current Day of Week Using weekday()

The following syntax illustrates how to return your current weekday from your actual date by using the weekday function.

my_weekday_1 = my_date.weekday()         # Applying weekday function
print(my_weekday_1)                      # Show weekday (in number format)
# 4

As you can see on the output above, the number 4 is returned. So in this case, the actual weekday is Friday (Monday is indexed as 0 and Sunday as 6).

 

Example 2: Get Actual Day of Week Applying isoweekday()

This section demonstrates how to show the actual weekday with the help of the isoweekday function:

my_weekday_2 = my_date.isoweekday()      # Using isoweekday function
print(my_weekday_2)                      # Print number of weekday
# 5

Considering the result of the first example, we now get the output 5 since the isoweekday relies on a different index system (1-7, representing Monday to Sunday).

 

Example 3: Get Current Day of Week as Name

The following Python syntax demonstrates how to get the current weekday as name instead of integer like in the previous two examples.

To return the full name of the day, we can use the strftime function combined with the format code ‘%A’:

my_weekday_3 = my_date.strftime('%A')    # Use strftime function
print(my_weekday_3)                      # Return day as name
# Friday

Looks good! If you want the output to be an abbreviated weekday name (Sun, Mon, …) instead of the full weekday name, simply use the format code ‘%a’.

 

Video & Further Resources

In case you need further info on the content of the present tutorial, I recommend watching the following video on my YouTube channel. I’m explaining the topics of this article in the video.

 

The YouTube video will be added soon.

 

In addition, you may want to have a look at the related tutorials on this website. You can find some tutorials below:

 

In this article you have learned how to return the current day of the week in number and name in Python. Please let me know in the comments, in case you have further questions.

 

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.

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

Menu
Top