How to convert local time string to UTC?

后端 未结 23 1178
离开以前
离开以前 2020-11-22 04:18

How do I convert a datetime string in local time to a string in UTC time?

I\'m sure I\'ve done this before, but can\'t find it and SO will hopefull

23条回答
  •  [愿得一人]
    2020-11-22 04:56

    if you prefer datetime.datetime:

    dt = datetime.strptime("2008-09-17 14:04:00","%Y-%m-%d %H:%M:%S")
    utc_struct_time = time.gmtime(time.mktime(dt.timetuple()))
    utc_dt = datetime.fromtimestamp(time.mktime(utc_struct_time))
    print dt.strftime("%Y-%m-%d %H:%M:%S")
    

提交回复
热议问题