To get the day of the week in Python, the easiest way is to use the datetime weekday() function. weekday() returns a number between 0 and 6 where 0 represents Monday and 6 represents Sunday.

from datetime import datetime

now = datetime.now()
day_of_week = now.weekday()

print(now)
print(day_of_week)

#Output:
2022-10-07 08:46:25.705684
4

Another way you can get the day of the week in Python is with the datetime isoweekday() function. isoweekday() returns a number between 1 and 7 where 1 represents Monday and 7 represents Sunday.

from datetime import datetime

now = datetime.now()
day_of_week = now.isoweekday()

print(now)
print(day_of_week)

#Output:
2022-10-07 08:46:25.705684
5

To get the name of the weekday, you can use the stftime() function and pass “%A”.

from datetime import datetime

now = datetime.now()
name_of_day_of_week = now.strftime("%A")

print(now)
print(name_of_day_of_week)

#Output:
2022-10-07 08:46:25.705684
Friday

One other way you can get the name of the weekday is with the calendar module day_name list.

from datetime import datetime
import calendar

now = datetime.now()
name_of_day_of_week = calendar.day_name[now.weekday()]

print(now)
print(name_of_day_of_week)

#Output:
2022-10-07 08:46:25.705684
Friday

When working with date variables, the ability to extract different pieces of information about those dates easily is valuable.

One such case is if you want to find the day of the week of a date in Python.

There are a few different ways you can get the day of the week in Python. If you want to get the number of the day of the week, there are two options.

To get the day of the week, you can use the datetime weekday() and datetime isoweekday() functions.

weekday() returns a number between 0 and 6 where 0 represents Monday and 6 represents Sunday, while isoweekday() returns a number between 1 and 7 where 1 represents Monday and 7 represents Sunday.

Below shows a simple example of how you can get the day of the week with weekday() and isoweekday() in Python.

from datetime import datetime

now = datetime.now()
day_of_week = now.weekday()
iso_day_of_week = now.isoweekday()

print(now)
print(day_of_week)
print(iso_day_of_week)

#Output:
2022-10-07 08:46:25.705684
4
5

Get Name of Day of Week in Python

If you want to get the name of the day of the week in Python, then you have two options.

To get the day name of the day of the week, you can use the datetime strftime() function or use the calendar module day_name list.

Below shows you some simple examples of how you can get the name of the day of the week using stftime() and the calendar module in Python.

from datetime import datetime
import calendar

now = datetime.now()
name_day_of_week_str = now.strftime("%A")
name_day_of_week_cal = calendar.day_name[now.weekday()]

print(now)
print(name_day_of_week_str)
print(name_day_of_week_cal)

#Output:
2022-10-07 08:46:25.705684
Friday
Friday

Hopefully this article has been useful for you to learn how to get the day of the week in Python.

Categorized in:

Python,

Last Update: March 1, 2024