Python: What day of the week today?

Today (2020-09-19) is Saturday but how can we get the value in Python? Before explaining, let's see Python calendar package.

import calendar

days = calendar.day_name
days_list = list(days)

print(days)
# <calendar._localized_day object at 0x117b172b0>

print(days_list)
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

calendar.day_name itself is not a list but can be cast to a list from Monday to Sunday.

What day of the week today?

import calendar
import datetime

today = datetime.date.today()

print(today)  # 2020-09-19
print(type(today))  # <class 'datetime.date'>

weekday = today.weekday()

print(weekday)  # 5
print(type(weekday))  # <class 'int'>

day_name = calendar.day_name[weekday]

print(day_name)
# Saturday

today returns the today datetime.date object. weekday is an index of day_name.

Python Date & Time

Python Tutorial