【问题标题】:How to get on python current time? [duplicate]如何获取python当前时间? [复制]
【发布时间】:2017-10-01 10:31:42
【问题描述】:

我需要知道日期。 时间的格式应该是2017-01-18T07:34:35Z 我已经尝试过了,但它不起作用:

import datetime
i = datetime.datetime.now()
date = %i.year "-" %i.month "-" %i.day "T" %i.hour ":" %i.minute ":" %i.second "Z"

【问题讨论】:

  • 请阅读datetime的文档,尤其是关于strftime的部分。
  • 实际上很容易将时间格式化为您想要的格式。网络上的例子数不胜数。我只想谷歌(python 日期时间)并查看所有非常有用的示例,其中许多示例还会在 StackOverflow 上返回 Q/A。请阅读How to ask a good question

标签: python date time


【解决方案1】:
from time import gmtime, strftime
strftime("%a, %d %b %Y %H:%M:%S", gmtime())

# Will output
'Thu, 03 May 2017 16:21:01'

您的示例使用:

from time import gmtime, strftime
strftime("%y-%d-%mT%H:%M:%SZ", gmtime())
2017-03-05T07:34:35Z

【讨论】:

    【解决方案2】:
        import time
    
        now = time.strftime("%c")
    

    https://www.cyberciti.biz/faq/howto-get-current-date-time-in-python/中查看更多信息

    【讨论】:

    • Nice 不知道 strftime(%c),但 OP 希望在时间之前使用“T”,之后使用“Z”进行转换
    • "T" + time.strftime("%x") and time.strftime("%X") + "Z"
    猜你喜欢
    • 2021-01-28
    • 2012-01-10
    • 2016-06-05
    • 2017-08-02
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多