[Python] Explain the difference between strftime and strptime in the datetime module with an example

[Memo] Difference between strftime and strptime in python's datetime module and how to remember them

Both are date and string conversions. Which is which? Eliminate the hesitation.

① strftime: "date" → "character string" ② strptime: "character string" → "date"


## Easy way to remember The difference between the two is "f" or "p".

-The "f" of strftime is regarded as "from". └ str from time: Character string from time (date)

・ Strptime is not "from" └ str time: string to date


## Remember from the formal meaning ・ "F" of strftime is "format" └ format: Change format └ Only character strings have a format (date data is standard)

・ "P" of strptime is "purse" └ purse: analyze └ What needs to be parsed is a string (date data is typed)

  • There are various theories about the meaning of "f" and "p". I'm writing something that fits nicely.

## How to use strftime and strptime

** Reference ** -Strftime: How to change the date format (date → character string) -Strptime: How to convert character string data to date (character string → date)


## List of date specifiers

** ■ Frequently used specifiers **

Specifier meaning
%Y 4 digits in the Christian era(yyyy)
%y 2 digits in the Christian era(yy)
%m Month (with 0 padding)
%d Day (with 0 padding)
%H Hours (24 hours,0 filled)
%I Time (12 hours,0 filled)
%M Minutes (0 padded)
%S Seconds (with 0 padding)

** ■ No 0 padding (different for windows and mac) ** windows: Add "#"
Specifier meaning
%#m Month (no 0 padding)
%#d Day (no 0 padding)
%#H Hours (24 hours,0 not filled)
%#I Time (12 hours,0 not filled)
%#M Minutes (no 0 padding)
%#S Seconds (without zero padding)

mac: Add "-"

Specifier meaning
%-m Month (no 0 padding)
%-d Day (no 0 padding)
%-H Hours (24 hours,0 not filled)
%-I Time (12 hours,0 not filled)
%-M Minutes (no 0 padding)
%-S Seconds (without zero padding)

** ■ Other specifiers (example) **
Specifier meaning
%f Microseconds(1~6 digits)
%a Day of the week(Shortened form)
%A Day of the week(full)
%w Day of the week(0~6「0:Sun ~ 6:Month")
%j date(1-366 days, 0 fill)
%W Week number(0~53, 0 fill)
%U %Same as W
%% "" As a character string%」

For details, refer to [Official page](https://docs.python.org/ja/3/library/datetime.html#id7)

Recommended Posts

[Python] Explain the difference between strftime and strptime in the datetime module with an example
About the difference between "==" and "is" in python
Difference between list () and [] in Python
Difference between == and is in python
Python module num2words Difference in behavior between English and Russian
difference between statements (statements) and expressions (expressions) in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
How to get the date and time difference in seconds with python
[python] Difference between variables and self. Variables in class
Let's explain the asset allocation by the Black-Litterman model (with an execution example by Python)
[python] Calculation of months and years of difference in datetime
Difference between return, return None, and no return description in Python
Let's explain the difference in how the new coronavirus spreads between Japan and other countries with a cluster countermeasure model
Find the difference in Python
Get the current date and time in Python, considering the time difference
Difference between Ruby and Python split
Cooperation between python module and API
Differences between Python, stftime and strptime
Difference between python2 series and python3 series dict.keys ()
[Python] Explains how to use the format function with an example
[Python3] Save the mean and covariance matrix in json with pandas
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python
[Python] Difference between function and method
[Python] Comprehension notation. Write a for statement simply. (A collection is the difference between an iterator and an iterator)
File open function in Python3 (difference between open and codecs.open and speed comparison)
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
Master the weakref module in Python
Write a C language linked list in an object-oriented style (with code comparison between Python and Java)
Fill the string with zeros in python and count some characters from the string
[Introduction to Python] What is the difference between a list and a tuple?
Comparing the basic grammar of Python and Go in an easy-to-understand manner
I set the environment variable with Docker and displayed it in Python
I tried to convert datetime <-> string with tzinfo using strftime () and strptime ()
Get and convert the current time in the system local timezone with python
Open an Excel file in Python and color the map of Japan
Create a record with attachments in KINTONE using the Python requests module
An example of the answer to the reference question of the study session. In python.
Communicate between Elixir and Python with gRPC
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
python> link> strftime () and strptime () behavior / code
Implementation module "deque" in queue and Python
Differences in syntax between Python and Java
Difference between PHP and Python finally and exit
Display Python 3 in the browser with MAMP
[Python] Difference between class method and static method
Dealing with "years and months" in Python
[python] Difference between rand and randn output
Differences in multithreading between Python and Jython
Module import and exception handling in python
Install the Python module in any directory
I tried to explain how to get the article content with MediaWiki API in an easy-to-understand manner with examples (Python 3)
Python datetime How to calculate dates and convert strings strftime, strptime [Definitive Edition]
Difference in writing method to read external source code between Ruby and Python
To automatically send an email with an attachment using the Gmail API in Python
What is the difference between `pip` and` conda`?
[Python] Get the files in a folder with Python