1

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.

Community
  • 1
  • 1
ComputerLocus
  • 3,448
  • 10
  • 47
  • 96

1 Answers1

4

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
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Note: in general, one day ago may be different from 24 hour. Though they are the same for UTC timezone. – jfs Feb 28 '14 at 08:45