ComputerLocus
ComputerLocus

Reputation: 3618

Getting the RFC 3339 timestamp for a day ago

Looking at this answer I can get the RFC 3339 based time fairly easily as the code for it shows:

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

I am wondering how I would get the same format of time, but for exactly one day ago. It would essentially be day-1, however I am not sure how to do this.

Upvotes: 1

Views: 3481

Answers (1)

paxdiablo
paxdiablo

Reputation: 881083

You can get one day previous to x with:

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

The following transcript shows this in action:

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

Upvotes: 4

Related Questions