5

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

It it other method to get weekday name in Python that setting date to monday and using strftime('%a') than adding timedelta(day=1).

Chameleon
  • 9,722
  • 16
  • 65
  • 127

2 Answers2

3

From this answer:

You can use

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

Where Monday = 0, Tuesday = 1 etc.

Community
  • 1
  • 1
Ryan Jackman
  • 780
  • 2
  • 8
  • 24
1

You can get the weekday names like this:

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 bit unsatisfactory that you cannot specify the weekday as an integer variable. In my environment you can do it like this, as it happens:

locale.nl_langinfo(locale.DAY_1 + x)

but the docs don't guarantee it.

Pelle Nilsson
  • 970
  • 8
  • 10