从 Weekday int 获取 Day 名称

我有一个工作日整数(0,1,2...) ,我需要得到日名(‘ Monday’,‘ Monday’,...)。

是否有内置的 Python 函数或实现方法?

下面是我编写的函数,它可以工作,但是我想从内置的 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"
100553 次浏览

It's very easy:

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

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.

I have been using calendar module:

import calendar
calendar.day_name[0]
'Monday'

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']

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"

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

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

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.  "))))

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]