且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从工作日int获取日期名称

更新时间:2023-11-05 16:59:10

更多Pythonic可以使用日历模块

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'

(如果星期一不是第一天)在一周中,请使用 setfirstweekday 进行更改)

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