[PYTHON] Get date and time in specified format

Preface There are many occasions when you need the date and time when dealing with data or when retrieving logs, but a memo to get the text in the format you want from datetime.

Environment Windows10 18363.1139 Python 3.9.0 Visual Studio Code 1.5.0

Reference [datetime — Basic date and time types](https://docs.python.org/3/library/datetime.html)

Sample code

sample.py


from datetime import datetime

timestamp = datetime.now().strftime('%Y year%m month%d day%H o'clock%M minutes%S seconds')
print(timestamp)

Result ``` October 18, 2020 15:45:50 ```

Supplement For directives that can be used other than the sample, refer to `strftime () and strptime () Behavior` on the reference page.

Recommended Posts

Get date and time in specified format
Get the current date and time in Python, considering the time difference
Determine the date and time format in Python and convert to Unixtime
Get date in Python
To represent date, time, time, and seconds in Python
How to get the date and time difference in seconds with python
Convert timezoned date and time to Unixtime in Python2.7
Date and time ⇔ character string
[Python] Create a date and time list for a specified period
Get the MIME type in Python and determine the file format
Released erajp that displays date time in Japanese calendar format in Python
Get the title and delivery date of Yahoo! News in Python
Format the Git log and get the committed file name in csv format
Get JST current time in ISO 8601 notation
Pytest Current time test (fixed date and time)
Get and convert the current time in the system local timezone with python
How to stop a program in python until a specific date and time
Get time series data from k-db.com in Python
Get Unix time of the time specified by JST regardless of the time zone of the server in Python
Recursively get website titles and URLs in Scrapy
format in python
Get drawing information every set time in Tkinter
[Python] Rename all image files in a specific folder by shooting date and time
Extract "current date only" and "current date and time" with python datetime.
Convert date timezone (time difference) in Python (from string)
Get standard output in real time with Python subprocess
Get the result in dict format with Python psycopg2
Save TOPIX time series in pickle, csv, Excel format
Get your current location and user agent in Python
Save and manage Qiita articles in Zenn format Markdown
Find this week's date in any format with python
Get stock prices and create candlestick charts in Python
Get a participant's username and screen name in Slack
Summary of date processing in Python (datetime and dateutil)
How to get RGB and HSV histograms in OpenCV
Date manipulation in Python
Image format in Python
[Python3] Get date diff
Date calculation in python
Get date with python
Date calculation in Python
python get current time
[Go language] How to get terminal input in real time
Get and create nodes added and updated in the new version
Search for variables in pandas.DataFrame and get the corresponding row.
I tried to illustrate the time and time in C language
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
How to get all the keys and values in the dictionary
Get YouTube Live chat field in real time with API
[Python] Strengths and weaknesses of DataFrame in terms of time required
Pandas basics for beginners ④ Handling of date and time items
Graph time series data in Python using pandas and matplotlib
It is surprisingly troublesome to get a list of the last login date and time of Workspaces