Convert date timezone (time difference) in Python (from string)

It's a memorandum because I was unexpectedly addicted to the pot. It seems to be easier, but once this is done.

What I want to do → I want to change the date (standard time) of the character string to Japan time

#This is all you need to import
import datetime

#Original date (standard time)
hiduke = '25/Aug/2020 11:01:52'

#1.Convert to date type
hiduke = datetime.datetime.strptime(hiduke, '%d/%b/%Y %H:%M:%S')

#2.Give time zone information (UTC here)
hiduke = hiduke.replace(tzinfo=datetime.timezone.utc)

#3.Convert time zone to Japan time
(+9 hours)
hiduke = hiduke.astimezone(datetime.timezone(datetime.timedelta(hours=+9)))

#4.Erase timezone notation
hiduke = hiduke.replace(tzinfo=None)

1. Convert to date type

--strrptime = String (to be) Replace (ed by) Time seems to be connected. --In the argument, specify the date (character string) you want to convert and the notation that the date is written. --% d = day --% b = month (Aug). Abbreviation for the name of the month. In the case of a number month,% m, in the case of August,% B --% Y = year --If it is 2020-08-26, it is written as % Y-% m-% d. --A list of notations can be found here. Convert dates, times and strings with Python datetime (strftime, strptime)

2. Give time zone information (UTC here)

tzinfo = timezone info.

3. Set the time zone to Japan time

Japan time is standard time + 9 hours. It becomes hours = + 9. The actual notation is datetime.timedelta (hours = + 9)

4. Erase the time zone notation

If you leave it as ③, +09: 00 (time zone notation) will be attached after the date, so you can delete it by setting tzinfo = None. With this, 25 / Aug / 2020 11:01:52 becomes 2020-08-25 20:01:52.

reference

Set / Get / Convert / Delete timezone with Python, datetime, pytz

Convert string <-> date (datetime) in Python

Recommended Posts

Convert date timezone (time difference) in Python (from string)
[python] Convert date to string
String date manipulation in Python
Get the current date and time in Python, considering the time difference
Determine the date and time format in Python and convert to Unixtime
Conversion of string <-> date (date, datetime) in Python
Generate a class from a string in Python
Convert from Markdown to HTML in Python
How to get the date and time difference in seconds with python
Get and convert the current time in the system local timezone with python
Get time series data from k-db.com in Python
Create a datetime object from a string in Python (Python 3.3)
To represent date, time, time, and seconds in Python
String manipulation in python
Date manipulation in Python
[Python2] Date string-> UnixTime-> Date string
Get date in Python
Date calculation in python
Date calculation in Python
Convert the world time zone time string to Japan time without calculating the time difference with python.
How to convert / restore a string with [] in python
Output the time from the time the program was started in python
Operators ++,-cannot be used in python (difference from php)
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
String object methods in Python
Find the difference in Python
OCR from PDF in Python
Date and time ⇔ character string
Convert CIDR notation in Python
Sort by date in python
Difference between list () and [] in Python
Difference between == and is in python
Convert markdown to PDF in Python
How to get a string from a command line argument in python
Measure function execution time in Python
Timezone specification when converting a string to datetime type in python
Extract text from images in Python
Python (from first time to execution)
6 ways to string objects in Python
Create a random string in Python
Code tests around time in Python
Released erajp that displays date time in Japanese calendar format in Python
Extract strings from files in Python
Fill the string with zeros in python and count some characters from the string
git / python> git log analysis (v0.1, v0.2)> calculate total working time in minutes from git log
Note: [Python3] Convert datetime to a string in any format you like
I want to batch convert the result of "string" .split () in Python
How to stop a program in python until a specific date and time
Python Date / Time Library Reverse Lookup Reference
Get exchange rates from open exchange rates in Python
difference between statements (statements) and expressions (expressions) in Python
Revived from "no internet access" in Python
Prevent double boot from cron in Python
Get date and time in specified format
Big difference in ruby, python, httpd performance
Just print Python elapsed time in seconds
Convert psd file to png in Python
Private Python handbook (updated from time to time)
Difference between @classmethod and @staticmethod in Python
# 5 [python3] Extract characters from a character string
Convert from katakana to vowel kana [python]