【问题标题】:Getting the RFC 3339 timestamp for a day ago获取一天前的 RFC 3339 时间戳
【发布时间】:2014-02-26 01:06:27
【问题描述】:

查看this 的答案,我可以很容易地获得基于 RFC 3339 的时间,因为它的代码显示:

d = datetime.datetime.utcnow() # <-- get time in UTC
print d.isoformat("T") + "Z"

我想知道如何获得相同格式的时间,但恰好是一天前。基本上是day-1,但我不知道该怎么做。

【问题讨论】:

    标签: python python-2.7 timezone rfc3339


    【解决方案1】:

    您可以通过以下方式获得x 的前一天:

    x = x + datetime.timedelta(days = -1)
    

    以下记录显示了这一点:

    pax> python
    Python 2.7.3 (default, Jan  2 2013, 16:53:07) 
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    
    >>> import datetime
    >>> d = datetime.datetime.utcnow()
    >>> d
    datetime.datetime(2014, 2, 26, 1, 11, 1, 536396)
    
    >>> print d.isoformat("T") + "Z"
    2014-02-26T01:11:01.536396Z
    
    >>> d = d + datetime.timedelta(days=-1)
    >>> d
    datetime.datetime(2014, 2, 25, 1, 11, 1, 536396)
    
    >>> print d.isoformat("T") + "Z"
    2014-02-25T01:11:01.536396Z
    

    【讨论】:

    • 注意:一般情况下,一天前可能与 24 小时不同。虽然它们对于 UTC 时区是相同的。
    猜你喜欢
    • 2013-02-09
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 2011-11-06
    • 2016-04-14
    • 1970-01-01
    相关资源
    最近更新 更多