Get Day name from Weekday int

Solution 1:

It is more Pythonic to use the calendar module:

>>> import calendar
>>> list(calendar.day_name)
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

Or, you can use common day name abbreviations:

>>> list(calendar.day_abbr)
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

Then index as you wish:

>>> calendar.day_name[1]
'Tuesday'

(If Monday is not the first day of the week, use setfirstweekday to change it)

Using the calendar module has the advantage of being location aware:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
'de_DE'
>>> list(calendar.day_name)
['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag']

Solution 2:

If you need just to print the day name from datetime object you can use like this:

current_date = datetime.now
current_date.strftime('%A')  # will return "Wednesday"
current_date.strftime('%a')  # will return "Wed"

Solution 3:

I have been using calendar module:

import calendar
calendar.day_name[0]
'Monday'

Solution 4:

It's very easy:

week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
week[weekday]