Fix Python – How to create a DateTime equal to 15 minutes ago?

Question

Asked By – Will Curran

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

Now we will see solution for issue: How to create a DateTime equal to 15 minutes ago?


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)

This question is answered By – Spacedman

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0