python - 将日期字符串转换为 UTC 日期时间

标签 python

我返回了两个给定日期之间的日期:

for date in rrule(DAILY, dtstart = date1, until = date2):
        print date_item.strftime("%Y-%m-%d")

如何转换date_item 例如2016-01-01 转换为 ISO 8601 格式,例如:Python 中的2016-01-01T18:10:18.000Z

最佳答案

我创建了一个示例日期时间对象,如下所示:

from datetime import datetime 
now = datetime.now()
print now
2016-09-21 16:59:18.175038

这是根据您的要求格式化的输出:

print datetime.strftime(now, "%Y-%m-%dT%H:%M:%S.000Z")
'2016-09-21T16:59:18.000Z'

这是一个很好的网站来引用各种选项,我个人觉得它比Python的官方文档更容易使用: http://strftime.org

关于python - 将日期字符串转换为 UTC 日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39615093/

相关文章:

解析元组的 Pythonic 方式

python - 从Python中的前缀树返回最相似的位签名

python - 使用 Google API 获取用户电子邮件的最佳方式是什么?

python - 是否可以循环对嵌套字典中列表中的值求和?

python - 更新了 : reshape each row data into a (x, 1) 数组

python - pymysql - pymysql.err.InternalError : 1054, 用户输入字符串用作列名

python - Numpy 逐元素比较

python - 简单的 wxPython 框架内容调整大小 - 比率?

python - 如何在python中重置for循环的索引号

python - 为什么python 2.7中的字符串没有 "__iter__"属性,但python 3.7中的字符串有 "__iter__"属性