美文网首页
Python 本地 日期时间 转 UTC 日期时间

Python 本地 日期时间 转 UTC 日期时间

作者: Hello泽泽 | 来源:发表于2020-04-02 16:08 被阅读0次
#!/usr/bin/env python
#author:  linuxhub.cn
# coding=utf-8

import datetime

def local_to_utc(local_date_time):
    local_time = datetime.datetime.strptime(local_date_time,'%Y-%m-%d %H:%M')
    utc_time = local_time - datetime.timedelta(hours=8)
    utc_date_time = utc_time.strftime("%Y-%m-%dT%H:%M:%SZ")
    return utc_date_time


local_date_time="2020-04-02 15:46"

print "转换前: " + str(local_date_time)
print "转换后: " + str(local_to_utc(local_date_time))
# python demo.py
转换前: 2020-04-02 15:46
转换后: 2020-04-02T07:46:00Z

相关文章

网友评论

      本文标题:Python 本地 日期时间 转 UTC 日期时间

      本文链接:https://www.haomeiwen.com/subject/sqixphtx.html