看着 答案我可以很容易地获得基于RFC3339的时间,就像它的代码所示:

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

我想知道我将如何获得相同的时间格式,但正好一天前。它基本上是 day-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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top