How to get week name in locale format from number in Python?

How to get week name in locale format from number in Python?

This is a different way to get the week name in Python, setting the date on Monday and using strftime('%a')than adding timedelta(day=1).

+4
source share
2 answers

From this answer :

you can use

>>> import calendar
>>> dayoftheweek = 2
>>> calendar.day_name[dayoftheweek]
'Wednesday'

Where Monday = 0, Tuesday = 1etc.

+2
source

You can get weekday names as follows:

import locale

locale.nl_langinfo(locale.DAY_1)
locale.nl_langinfo(locale.DAY_2)
locale.nl_langinfo(locale.DAY_3)
locale.nl_langinfo(locale.DAY_4)
locale.nl_langinfo(locale.DAY_5)
locale.nl_langinfo(locale.DAY_6)
locale.nl_langinfo(locale.DAY_7)

Still a little unsatisfactory that you cannot specify the day of the week as an integer variable. In my environment, you can do it like this:

locale.nl_langinfo(locale.DAY_1 + x)

but the documents do not guarantee it.

+1
source

All Articles