How to convert local time string to UTC?

后端 未结 23 1172
离开以前
离开以前 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 05:02

    I found the best answer on another question here. It only uses python built-in libraries and does not require you to input your local timezone (a requirement in my case)

    import time
    import calendar
    
    local_time = time.strptime("2018-12-13T09:32:00.000", "%Y-%m-%dT%H:%M:%S.%f")
    local_seconds = time.mktime(local_time)
    utc_time = time.gmtime(local_seconds)
    

    I'm reposting the answer here since this question pops up in google instead of the linked question depending on the search keywords.

提交回复
热议问题