How to Get the Day of the Week in Python?

Python provides various modules to deal with date and time such as a time module, a datetime module, a calendar module, etc. Getting the day of the week is essential in Python or any other programming language for various purposes such as scheduling tasks, performing operations based on weekdays, etc.

Python offers strftime(), weekday(), and isoweek() methods, etc to get the day of the week. All these methods are discussed in this guide with the help of examples.

Method 1: Using the strftime() Method

The “strftime()” method is used in Python to convert the specified date into a string. This function accepts multiple format codes as a parameter and retrieves a formatted string based on it. 

Here is an example code:

Code:

import datetime
print(datetime.datetime.today().strftime('%A'))
  • The above code the “datetime.today()” function is used along with the “strftime()” function.
  • The “datetime.today()” function returns a datetime object representing the current/present local date and time.
  • The “strftime()” function takes the format code “%A” as an argument and returns the name of the day.

Output:

The day of the week has been printed.

Method 2: Using the weekday() Method

The “weekday()” method is used in Python to get the weekday as an integer, where Monday is 0 and Sunday is 6. 

Here is an example code:

Code:

import datetime
print(datetime.datetime.today().weekday())
  • The module named “datetime” is imported.
  • The “datetime.today()” function is used to get the current day while the “weekday()” function is used to get the weekday as an integer type.

Output:

The “2” number represents the third day of the week which is “Wednesday”.

Method 3: Using Pandas Timestamp Method

The “Timestamp()” function and “day_name()” function of the Pandas module is used to get the day of the week. 

Here is an example:

Code:

import pandas
current_date = pandas.Timestamp.today()
print('Day of the Week: ', current_date.dayofweek)
print('Day Name of the Week: ', current_date.day_name())
  • The module named “pandas” is imported.
  • The “pd.Timestamp.today()” function is used to get the current date.
  • The “dayofweek” attribute of Pandas function is utilized to get the day of the week of the current date.
  • The “day_name()” function gets the name of the day.

Output:

The day of the week in integer “2” and with the name “Wednesday” has been printed successfully.

Method 4: Using the calendar Module

To work with calendars the classes and functions of the calendar module are used in Python. The below code uses the “calendar.day_name” method of the calendar module to get the day name of the week: 

Code:

import datetime, calendar
current_date = datetime.date.today()
print(calendar.day_name[current_date.weekday()])
  • The datetime and calendar module is imported.
  • The “datetime.date.today()” function is used to get the current or today date.
  • The “calendar.day_name[]” method is utilized along with the “curren_date.weekday()” function to get the specified day of the week.

Output:

The day of the week has been printed.

Method 5: Using the isoweekday() Method

The “isoweekday()” method is utilized to get/find the day of the given date in the form of an integer. 

Here is an example code:

Code:

import datetime
print(datetime.datetime.today().isoweekday())

The “datetime.today()” function is used to get the current date and “isoweekday()” function is used to get the day of the week.

Output:

The input day of the week has been displayed.

Conclusion

To get the day of the week, the strftime() method, weekday() method, Pandas Timestamp method, “calendar” module and isoweekday() method are used in Python. The “strftime()” function is used along with the “today()” function to get the day of the week. Similarly, the “weekday()” and “isoweekday()” methods get the day of the week by returning the integer data type. The “Pandas Timestamp method” is used to find the day of the week in integers along with the specific day’s name.

This guide presented various methods to get the day of the week in Python.