89

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"
GER
  • 1,870
  • 3
  • 23
  • 30
  • Going to the docs is always the first step if you want to use a datetime method. But from my experience, if you want to use a datetime method then you'll need to start creating datetime.datetime or datetime.date objects so your weekday integers would need to change to datetime objects. – Dzhao Mar 31 '16 at 19:02

9 Answers9

170

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']
dawg
  • 98,345
  • 23
  • 131
  • 206
  • 1
    Good explanation. I did not know about Calendar library. – GER Mar 31 '16 at 19:18
  • 1
    Thank you! This library is in need of better documentation. I had to look into the code to find out that `calendar.day_name`has a `__getitem__` method that slices a list with datetime objects representing a week (`calendar.day_name._days`) and returns a formated string representing the name of the day or a list if you pass in a slice. – dasdachs Nov 23 '16 at 12:57
  • This is excellent for pandas - reindexing by day after groupping, to have the days in order: `data.groupby('day').size().reindex(calendar.day_name).plot.bar()` - plot a weekly distribution of your data. (the `day` column contains the weekday name) – Tomasz Gandor Mar 28 '19 at 06:27
28

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"
Nik.exe
  • 391
  • 3
  • 5
19

I have been using calendar module:

import calendar
calendar.day_name[0]
'Monday'
zondo
  • 19,901
  • 8
  • 44
  • 83
Sharpowski
  • 617
  • 1
  • 7
  • 21
  • 2
    Better than hardcoding the daynames into your code. `calendar` also works with `locale` so the day names will automatically adjust if you want. – martineau Mar 31 '16 at 19:06
2

It's very easy:

week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
week[weekday]
xiº
  • 4,605
  • 3
  • 28
  • 39
2

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.

Aaron Christiansen
  • 11,584
  • 5
  • 52
  • 78
1

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

Samarth Saxena
  • 1,121
  • 11
  • 18
0

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
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
0

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.  "))))
saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
0

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]
vestland
  • 55,229
  • 37
  • 187
  • 305