获取一天前的RFC3339时间戳
-
21-12-2019 - |
题
看着 这 答案我可以很容易地获得基于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
不隶属于 StackOverflow