python datetime时区转换

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)

你可能感兴趣的:(python datetime时区转换)