python datetime时区转换

时间:2021-06-16 18:51:44
from pytz import timezone
def datetime_as_timezone(date_time, time_zone):
tz = timezone(time_zone)
utc = timezone('UTC')
return date_time.replace(tzinfo=utc).astimezone(tz) def datetime_to_str(date_time):
date_time_tzone = datetime_as_timezone(date_time, 'Asia/Shanghai')
return '{0:%Y-%m-%d %H:%M}'.format(date_time_tzone)