I investigated how to get the day of the week in Python, so it is a summary as a memorandum.
The following is a reference page.
It seems that there are two main methods.
--How to use the strftime ()
method of the datetime
module
--How to use the weekday ()
method of the datetime
module and the day_name
of the calendar
module
Let's take a closer look at each.
strftime ()
method of the datetime
module** strftime ()
** of the datetime module is a method for converting and formatting a date type or datetime type object to a string type.
If you pass the format you want to convert to an argument, it will be converted to a string.
% 〇
is called a formatting code, and you can embed the year, month, minute, day of the week, etc. here.
The format code for the day of the week is as follows.
--% A
: Day of the week name
--% a
: Day of the week name (short form)
If you want to get the day of the week in English, you can easily get it as follows.
import datetime as dt
date = dt.date(2001, 1, 2)
print(date.strftime('%Y-%m-%d')) # => '2001-01-02'
#Get the day of the week
print(date.strftime('%A')) # => 'Tuesday'
print(date.strftime('%a')) # => 'Tue'
The locale
module is a module for checking and changing the locale on Python, and you can change the locale usinglocale.setlocale ()
. After changing the locale to Japanese environment, you can get the Japanese day of the week by using strftime ()
as in the above code.
import datetime as dt
import locale
#Locale of time with locale module'ja_JP.UTF-8'Change to
locale.setlocale(locale.LC_TIME, 'ja_JP.UTF-8')
date = dt.date(2001, 1, 2)
print(date.strftime('%A')) # => 'Tuesday'
print(date.strftime('%a')) # => 'fire'
weekday ()
method of the datetime
module and the day_name
of the calendar
moduleThe weekday ()
of the datetime
module returns an integer corresponding to the day of the week. Monday starts at 0 and Sunday ends at 6. By the way, the datetime
module also has a method called ʻisoweekday ()`, which starts on Monday and ends on Sunday at 7.
The day_name ()
of the calender
module returns a sliceable type of calendar._localized_day
for the day of the week (English), so you can get the day of the week by combining weekday ()
and day_name
.
import calendar
import datetime as dt
import locale
print((2001, 1, 2).weekday()) # => 1
print(calendar.day_name[:]) # => ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
#Locale of time with locale module'ja_JP.UTF-8'Change to
locale.setlocale(locale.LC_TIME, 'ja_JP.UTF-8')
print(calendar.day_name[:]) # => ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
Use what was mentioned earlier.
import calendar
import datetime as dt
date = dt.date(2001, 1, 2)
#Get the day of the week
day_index = date.weekday() # => 1
print(calendar.day_name[day_index]) # => 'Tuesday'
To get it in Japanese, use locale.setlocale ()
to change the locale.
import calendar
import datetime as dt
import locale
#Locale of time with locale module'ja_JP.UTF-8'Change to
locale.setlocale(locale.LC_TIME, 'ja_JP.UTF-8')
date = dt.date(2001, 1, 2)
#Get the day of the week
day_index = date.weekday() # => 1
print(calendar.day_name[day_index]) # => 'Tuesday'
Either way works fine, but if you don't want to change the locale because you're changing the locale (only in the code you ran, not system-wide), you can use locale.setlocale ()
. It seems good to define and use a function that converts from an English day of the week to a Japanese day of the week without using it.
Recommended Posts