Python获取UTC时间

from datetime import datetime, timedelta
now_time = datetime.now()
utc_time = now_time - timedelta(hours=8)  # 减去8小时
utc_time = utc_time.strftime("%Y-%m-%d %H:%M:%S")
print(utc_time)

第二种:

from datetime import datetime
from pytz import utc
import time

# 本地时间
dt_loc = datetime.strptime("2020-07-09 18:21:17", "%Y-%m-%d %H:%M:%S")
print(type(dt_loc))
print(dt_loc)

# 本地时间转UTC时间
dt_utc = dt_loc.astimezone(utc)

# UTC时间转本地时间
dt_loc1 = dt_utc.astimezone()

print(dt_utc)
print(dt_loc1)

  

  

posted @ 2022-09-19 19:34  lucky_tomato  阅读(1274)  评论(0编辑  收藏  举报