Get the day of the week or month from a date in Python as a string (e.g. German or English)

Money and Business

Using Python's standard library datetime, you can create a datetime object from a date string and get the name of the day of the week or month from it as a string. However, the language of those strings depends on the locale (country or region setting) of the environment.

Here are two ways to get the name of the day of the week or month from a date as a string in any language.

  • Change the locale with the locale module
  • Define a new function

For more information on the basic usage of the datetime module and the methods strptime() and strftime() for converting between date and time (date, time) and strings, please refer to the following articles.

Change locale with locale module

The Python standard library provides a locale module to control the locale settings.

It depends on the environment, but in the example environment, using the following formatting code in the strftime() method, the names of the days of the week and months can be obtained in English notation.
%A, %a, %B, %b

The following example uses a datetime object to represent date and time (date and time), but the same is true for a date object that only has date information.

import datetime
import locale

dt = datetime.datetime(2018, 1, 1)
print(dt)
# 2018-01-01 00:00:00

print(dt.strftime('%A, %a, %B, %b'))
# Monday, Mon, January, Jan

LC_TIME, the locale category setting for time formatting, is checked with locale.getlocale(), and it is set to None. This result depends on the environment.

print(locale.getlocale(locale.LC_TIME))
# (None, None)

LC_TIME to Japanese (UTF-8) ja_JP.UTF-8 in locale.setlocale() to get the day and month names in Japanese. locale.LC_ALL can be used to set all locale categories, but note that this will affect, for example LC_MONETARY, for example.

Note that these changes are only effective in this code. It does not mean that the system environment variables will be rewritten.

locale.setlocale(locale.LC_TIME, 'ja_JP.UTF-8')
print(locale.getlocale(locale.LC_TIME))
# ('ja_JP', 'UTF-8')

print(dt.strftime('%A, %a, %B, %b'))
# 月曜日, 月, 1月,  1

You can also change the locale settings to use other language notations, such as English or German.

locale.setlocale(locale.LC_TIME, 'en_US.UTF-8')
print(dt.strftime('%A, %a, %B, %b'))
# Monday, Mon, January, Jan

locale.setlocale(locale.LC_TIME, 'de_DE.UTF-8')
print(dt.strftime('%A, %a, %B, %b'))
# Montag, Mo, Januar, Jan

If you want to get the day of the week for a given date from a date string in any language, you can do so by following the steps below.

  • LC_TIME to the value of the desired language setting (e.g. ja_JP.UTF-8) in locale.setlocale()
  • Converting a string to a datetime object with strptime()
  • Call strftime() on that datetime object with the following formatting code: %A, %a, %B, %b
locale.setlocale(locale.LC_TIME, 'ja_JP.UTF-8')

s = '2018-01-01'
s_dow = datetime.datetime.strptime(s, '%Y-%m-%d').strftime('%A')

print(s_dow)
# 月曜日

Define a new function

This can be achieved by defining a new function.

The weekday() method of the datetime object gives an integer value of 0 for Monday and 6 for Sunday.

import datetime

dt = datetime.datetime(2018, 1, 1)
print(dt)
# 2018-01-01 00:00:00

print(dt.weekday())
# 0

print(type(dt.weekday()))
# <class 'int'>

There is a similar method, isoweekday(), which returns an integer value of 1 for Monday and 7 for Sunday. Note that there is a subtle difference.

print(dt.isoweekday())
# 1

print(type(dt.isoweekday()))
# <class 'int'>

If we define a list of names of the days of the week for each language string and retrieve them using the integer values obtained by the weekday() method, we can achieve our goal.

Copied title and URL