LoginSignup
0
1

More than 1 year has passed since last update.

[python] ISO8601 UTC文字列からJST datetime変換

Posted at

pythonでUTC文字列をJST datetimeへ切り替える方法

from datetime import datetime, timezone, timedelta

str_utc = '2022-12-05T04:58:38Z'
dt_jst = datetime.strptime(str_utc, '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=timezone(timedelta(hours=0))).astimezone(tz=timezone(timedelta(hours=+9)))
print('{}'.format(dt_jst.strftime('%Y-%m-%d %H:%M:%S %z')))

以下出力

2022-12-05 13:58:38 +0900
  1. strptimeだとtimezoneが付けられないのでreplaceを利用してtimezoneとしてUTCを無理やりセット
  2. astimezoneを利用してtimezoneをJST(+0900)へ切り替え
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1