Python - string转datetime并设置时区

#先把字符串转成不带时区的datetime

dt = datetime.datetime.strptime('2018-02-03 15:37:12' , '%Y-%m-%d %H:%M:%S')

#把不带时区的datetime转成timestamp再转成秒

ts = int(dt.timestamp())

#把秒转成带时区的datetime

t = datetime.datetime.fromtimestamp(ts, pytz.timezone('Asia/Shanghai'))

你可能感兴趣的:(Python - string转datetime并设置时区)