Get Day name from Weekday int

Question:

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"
Asked By: GER

||

Answers:

It’s very easy:

week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
week[weekday]
Answered By: xiº

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.

Answered By: Aaron Christiansen

I have been using calendar module:

import calendar
calendar.day_name[0]
'Monday'
Answered By: Sharpowski

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']
Answered By: dawg

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"
Answered By: Nik.exe

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
Answered By: Anton vBR

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

Answered By: Samarth Saxena

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.  "))))
Answered By: priyabrata ray

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]
Answered By: vestland
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.