Python 从 Weekday int 获取日期名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36341484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Day name from Weekday int
提问by GER
I have a weekday integer (0,1,2...) and I need to get the day name ('Monday', 'Tuesday',...).
我有一个工作日整数 (0,1,2...),我需要获取日期名称 ('Monday', 'Tuesday',...)。
Is there a built in Python function or way of doing this?
是否有内置的 Python 函数或这样做的方法?
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"
回答by dawg
It is more Pythonic to use the calendar module:
使用日历模块更加 Pythonic :
>>> 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 setfirstweekdayto change it)
(如果星期一不是一周的第一天,请使用setfirstweekday更改它)
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']
回答by Sharpowski
I have been using calendar module:
我一直在使用日历模块:
import calendar
calendar.day_name[0]
'Monday'
回答by Nik.exe
If you need just to print the day name from datetime object you can use like this:
如果您只需要从 datetime 对象打印日期名称,您可以像这样使用:
current_date = datetime.now
current_date.strftime('%A') # will return "Wednesday"
current_date.strftime('%a') # will return "Wed"
回答by Aaron Christiansen
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:
如果您需要该函数在传入无效数字(例如“8”)时不会导致错误,则可以在返回之前检查列表中的该项目是否存在:
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.
我不确定是否有内置的方法可以做到这一点datetime
,但这仍然是一种非常有效的方法。
回答by xio
It's very easy:
这很容易:
week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
week[weekday]
回答by Anton vBR
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
回答by Samarth Saxena
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.weekday(year, month, day)- 返回年 (1970–…)、月 (1–12)、日 (1–31) 的星期几(0 是星期一)。
calendar.day_name- An array that represents the days of the week in the current locale
calendar.day_name- 一个数组,表示当前语言环境中的星期几
for more reference -https://docs.python.org/2/library/calendar.html#calendar.weekday
更多参考 - https://docs.python.org/2/library/calendar.html#calendar.weekday
回答by priyabrata ray
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. "))))