Timezone specification when converting a string to datetime type in python

datetime and timezone

When making a datetime type from a string and registering it in elasticsearch, It was strange if I didn't specify the timezone. It is the ``` memorandum``that I went to at that time. I would like to know if there is an easier way.

Before python3.7

--String format: YYYY-mm-dd HH: MM: SS + 0900 --Added + 0900 to the end (because it is UTC + 9 hours) --Strptime () format: % Y-% m-% d% H:% M:% S% z --% z corresponds to timezone

Example


from datetime import datetime as dt

#String
time_str = "2020-11-22 18:00:00+0900"
#Datetime type including timezone
time_dt = dt.strptime(time_str, '%Y-%m-%d %H:%M:%S%z')

print(time_dt.tzinfo)
# UTC+09:00

For python3.7

fromisoformat()Is available. Since the ISO 8601 format contains time zone information in the format `+ HH: MM``` or -HH: MM``` at the end, The argument is a character string similar to that.

python


#String
time_str = '2020-09-12 12:22:30+09:00'
#Datetime type including timezone
time_dt = dt.fromisoformat(time_str)

print(time_dt.tzinfo)
# UTC+09:00

Reference site

Set / get / convert / delete timezone with Python, datetime, pytz \ | note.nkmk.me

Recommended Posts

Timezone specification when converting a string to datetime type in python
When converting a string (a-> b) in Python, which is faster, if statement or dictionary type?
Python Note: When assigning a value to a string
Create a datetime object from a string in Python (Python 3.3)
How to handle datetime type in python sqlite3
Note: [Python3] Convert datetime to a string in any format you like
How to write a string when there are multiple lines in python
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
[Python] How to expand variables in a character string
Python --Notes when converting from str type to int type
Things to note when initializing a list in Python
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
Precautions when changing unix time to datetime type in pandas
6 ways to string objects in Python
Create a random string in Python
When writing a program in Python
How to make a string into an array or an array into a string in Python
[Introduction to Python] How to output a character string in a Print statement
What to do when the value type is ambiguous in Python?
Conversion of string <-> date (date, datetime) in Python
Precautions when pickling a function in python
[Python] How to invert a character string
How to get a stacktrace in python
Generate a class from a string in Python
When you want to replace multiple characters in a string without using regular expressions in python3 series
I want to color a part of an Excel string in Python
[Introduction to Python] Thorough explanation of the character string type used in Python!
Python2 string type
Python # string type
Try to calculate a statistical problem in Python
How to clear tuples in a list (Python)
To execute a Python enumerate function in JavaScript
Type Python scripts to run in QGIS Processing
converting argument $ 1 type: unsupported type [] string, a slice of string
I want to create a window in Python
How to create a JSON file in Python
A clever way to time processing in Python
Steps to develop a web application in Python
To add a module to python put in Julialang
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
Error when trying to install psycopg2 in Python
Behavior when saving python datetime object in MongoDB
Put out a shortened URL string in Python
Insert an object inside a string in Python
[Python] How to compare datetime with timezone added
I stumbled on the character code when converting CSV to JSON in Python
What to do when a warning appears around Python integration in Neovim's CheckHealth
Output timing is incorrect when standard (error) output is converted to a file in Python
[Python] What to check when you get a Unicode Decode Error in Django
[Python] I tried to get the type name as a string from the type function
Check if the string is a number in python
I want to easily implement a timeout in python
Things to keep in mind when developing crawlers in Python
Try to make a Python module in C language
How to write string concatenation in multiple lines in Python
Python callable type specification
I get a java.util.regex.PatternSyntaxException when splitting a string in PySpark
String manipulation in python