Get Day name from Weekday int

PythonDateDatetime

Python Problem Overview


I have a weekday integer (0,1,2...) and I need to get the day name ('Monday', 'Tuesday',...).

Is there a built in Python function or way of doing this?

Here is the function I wrote, which works but I wanted something from the built in datetime lib.

def dayNameFromWeekday(weekday):
    if weekday == 0:
        return "Monday"
    if weekday == 1:
        return "Tuesday"
    if weekday == 2:
        return "Wednesday"
    if weekday == 3:
        return "Thursday"
    if weekday == 4:
        return "Friday"
    if weekday == 5:
        return "Saturday"
    if weekday == 6:
        return "Sunday"

Python Solutions


Solution 1 - Python

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 - Python

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 - Python

I have been using calendar module:

import calendar
calendar.day_name[0]
'Monday'

Solution 4 - Python

It's very easy:

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

Solution 5 - Python

You could use a list which you get an item from based on your argument:

def dayNameFromWeekday(weekday):
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    return days[weekday]

If you needed the function to not cause an error if you passed in an invalid number, for example "8", you could check if that item of the list exists before you return it:

def dayNameFromWeekday(weekday):
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    return days[weekday] if 0 < weekday < len(days) else None

This function can be used like you'd expect:

>>> dayNameFromWeekday(6)
Sunday
>>> print(dayNameFromWeekday(7))
None

I'm not sure there's a way to do this built into datetime, but this is still a very efficient way.

Solution 6 - Python

You can use built in functions for that for ex suppose you have to find day according to the specific date.

import calendar

month,day,year = 9,19,1995
ans =calendar.weekday(year,month,day)

print(calendar.day_name[ans])

calendar.weekday(year, month, day) - Returns the day of the week (0 is Monday) for year (1970–…), month (1–12), day (1–31).

calendar.day_name - An array that represents the days of the week in the current locale

for more reference -https://docs.python.org/2/library/calendar.html#calendar.weekday

Solution 7 - Python

You can create your own list and use it with format.

import datetime

days_ES = ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"]

t = datetime.datetime.now()
f = t.strftime("{%w} %Y-%m-%d").format(*days_ES)

print(f)

Prints:

Lunes 2018-03-12

Solution 8 - Python

You can use index number like this:

days=["sunday","monday"," Tuesday", "Wednesday" ,"Thursday", "Friday", "Saturday"]
def date(i):
    
    return days[i]
print (date(int(input("input index.  "))))

Solution 9 - Python

Despite all elegant built-in solutions, I would prefer using a dictionary like:

{0:'mon', 1:'tue', 2:'wed', 3:'thur', 4:'fri', 5:'sat', 6:'sun'}
Details:

I find this to be a very suitable use-case for dictionaries since this will let you use whatever language or abbreviation that suits your needs, as well as setting any day to be the first day of the week. One example of a key, value pair could be daynames[0] = 'mon'. And all you need to set it up is one single line.

daynames = {0:'mon', 1:'tue', 2:'wed', 3:'thur', 4:'fri', 5:'sat', 6:'sun'}

Now, running daynames[1] will return 'tue'.

Setting it up like this also unleashes a bunch of neat possibilites with dicts. If you have a list or pandas series with unsorted integers likelst = [5, 3, 2, 6, 7,], just run:

[daynames[e] for e in lst]

And you'll get:

['sat', 'thur', 'wed', 'sun', 'tue']
Complete example:
daynames = {0:'mon',1:'tue',2:'wed',3:'thur',4:'fri',5:'sat',6:'sun'}
lst = [5, 3, 2, 6, 1]
[daynames[e] for e in lst]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGERView Question on Stackoverflow
Solution 1 - PythondawgView Answer on Stackoverflow
Solution 2 - PythonNik.exeView Answer on Stackoverflow
Solution 3 - PythonSharpowskiView Answer on Stackoverflow
Solution 4 - PythonxiºView Answer on Stackoverflow
Solution 5 - PythonAaron ChristiansenView Answer on Stackoverflow
Solution 6 - PythonSamarth SaxenaView Answer on Stackoverflow
Solution 7 - PythonAnton vBRView Answer on Stackoverflow
Solution 8 - Pythonpriyabrata rayView Answer on Stackoverflow
Solution 9 - PythonvestlandView Answer on Stackoverflow