查看this回答我可以很容易地获得基于RFC 3339的时间,因为它的代码显示:
d = datetime.datetime.utcnow() # <-- get time in UTC
print d.isoformat("T") + "Z"
我想知道如何获得相同的时间格式,但就在一天前。它本质上是day-1
,但我不知道该怎么做。
答案 0 :(得分:3)
您可以在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