[Python] Get the day of the week (English & Japanese)

How to get the day of the week in Python

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.

How to use the 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)

Get the day of the week in english

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'

Get Japanese day of the week

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'

How to use the weekday () method of the datetime module and the day_name of the calendar module

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

Get the day of the week in english

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'

Get Japanese day of the week

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'

important point

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

[Python] Get the day of the week (English & Japanese)
Get the last day of the specified month
Japanese translation: PEP 20 --The Zen of Python
Get the contents of git diff from python
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
the zen of Python
[Python] Get the last updated date of the website
Find out the day of the week with datetime
Get a datetime instance at any time of the day in Python
Get the update date of the Python memo file.
Get one day of the week from Zeller's joint ceremony ~ Incidentally, the perpetual calendar ~
Note: How to get the last day of the month with python (added the first day of the month)
Get the stock price of a Japanese company with Python and make a graph
How to get the number of digits in Python
[Python] Get the official file path of the shortcut file (.lnk)
[Python] Get the text of the law from the e-GOV Law API
[python] Get the list of classes defined in the module
Get the return code of the Python script from bat
Get the size (number of elements) of UnionFind in Python
[Python] Get the list of ExifTags names of Pillow library
Which day of the week do you buy gold?
Get the operation status of JR West with Python
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
Towards the retirement of Python2
About the ease of Python
Get the number of digits
The Power of Pandas: Python
Get Japanese synonyms in Python
Try to get the function list of Python> os package
Get the number of specific elements in a python list
Get the source of the page to load infinitely with python.
The day of docker run (note)
The story of Python and the story of NaN
Get the number of views of Qiita
First Python 3 ~ The beginning of repetition ~
Get the desktop path in Python
Existence from the viewpoint of Python
Get the weather with Python requests
Get the weather with Python requests 2
pyenv-change the python version of virtualenv
Get the script path in Python
How to get the Python version
Get the attributes of an object
Change the Python version of Homebrew
Get the first element of queryset
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
Get the number of Youtube subscribers
Japanese translation of the man-db manual
Get the desktop path in Python
Python Note: Get the current month
Japanese translation of the util-linux manual
About the basics list of Python basics
Japanese translation of the iproute2 manual
Learn the basics of Python ① Beginners
Note: Get the first and last items of Python OrderedDict non-destructively
March 14th is Pi Day. The story of calculating pi with python
[Python] How to get the first and last days of the month