Will Curran Will Curran - 2 months ago 11
Python Question

How to create a DateTime equal to 15 minutes ago?

I need to create a DateTime object that represents the current time minus 15 minutes.

Answer

import datetime and then the magic timedelta stuff:

In [63]: datetime.datetime.now()
Out[63]: datetime.datetime(2010, 12, 27, 14, 39, 19, 700401)

In [64]: datetime.datetime.now() - datetime.timedelta(minutes=15)
Out[64]: datetime.datetime(2010, 12, 27, 14, 24, 21, 684435)