Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo

Convert aware datetime object (UTC) to JST

>>> from pytz import timezone
>>> from datetime import datetime
>>> utc_now = datetime.now(timezone('UTC'))
>>> print(utc_now)
2016-03-20 01:07:29.056724+00:00
>>> jst_now = utc_now.astimezone(timezone('Asia/Tokyo'))
>>> print(jst_now)
2016-03-20 10:07:29.056724+09:00

Convert naive datetime objects to UTC, JST

>>> from pytz import timezone
>>> from datetime import datetime
>>> now = datetime.now()
>>> print(now)
2016-03-20 10:14:41.929706
>>> utc_now = timezone('UTC').localize(now)
>>> print(utc_now)
2016-03-20 10:14:41.929706+00:00
>>> jst_now = timezone('Asia/Tokyo').localize(now)
>>> print(jst_now)
2016-03-20 10:14:41.929706+09:00

Convert string date (UTC) to JST

>>> from pytz import timezone
>>> from dateutil import parser
>>> utc_string = "Sat Mar 19 06:17:57 +0000 2016"
>>> jst_time = parser.parse(utc_string).astimezone(timezone('Asia/Tokyo'))
>>> print(jst_time)
2016-03-19 15:17:57+09:00

Convert to ISO notation

>>> from datetime import datetime
>>> now = datetime.now()
>>> print(now)
2016-03-21 00:14:06.135787
>>> iso_now = now.isoformat()
>>> print(iso_now)
2016-03-21T00:14:06.135787

reference: Summary of how to convert between Python datetime object and Unix time Notes on dealing with time zones in Python Official document # datetime.isoformat

Recommended Posts

Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo
[Python beginner memo] Python character string, path operation
Python f character (formatted string)
Python QT app Standalone App conversion memo
python memo
Python memo
python memo
Python memo
Python string
Python memo
Python memo
Conversion of string <-> date (date, datetime) in Python
# 5 [python3] Extract characters from a character string
[Python] How to invert a character string
Basic grammar of Python3 system (character string)
Python basic course (4 numeric type / character string type)
String → Bool value conversion in Python Consideration
[Python] Memo dictionary
Python: String concatenation
Python string format
python beginner memo (9.2-10)
python string slice
python beginner memo (9.1)
python character code
[Python] Conversion memo between time data and numerical data
Character range / character string range
★ Memo ★ Python Iroha
Calculation of match rate of character string breaks [python]
[Python] EDA memo
Python 3 operator memo
[My memo] python
Python2 string type
Python3 metaclass memo
Python string format
Python # string type
[Python] Basemap memo
Python beginner memo (2)
Python string inversion
[Python] Numpy memo
Store Japanese (multibyte character string) in sqlite3 of python
[Python] How to expand variables in a character string
Python class (Python learning memo ⑦)
python openCV installation (memo)
Python module (Python learning memo ④)
Various character string operations
[Python] 2's complement conversion
Visualization memo by Python
String manipulation in python
Python test package memo
[Python] Memo about functions
[Python] Multi-line string assignment
Python string manipulation master
Hexadecimal string and string conversion
python regular expression memo
Binary search (python2.7) memo
[My memo] python -v / python -V
About Python3 character code
Python3 List / dictionary memo
[Memo] Python3 list sort
Python Tips (my memo)
[Python] Memo about errors