How to create a DateTime equal to 15 minutes ago?

PythonDatetime

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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)

Solution 2 - Python

 datetime.datetime.now() - datetime.timedelta(minutes=15)

Solution 3 - Python

This is simply what to do:

datetime.datetime.now() - datetime.timedelta(minutes = 15)

timedeltas are specifically designed to allow you to subtract or add deltas (differences) to datetimes.

Solution 4 - Python

import datetime 
datetime.datetime.now() - datetime.timedelta(0, 900)

Actually 900 is in seconds. Which is equal to 15 minutes. `15*60 = 900`

Solution 5 - Python

I have provide two methods for doing so for minutes as well as for years and hours if you want to see more examples:

import datetime
print(datetime.datetime.now())
print(datetime.datetime.now() - datetime.timedelta(minutes = 15))
print(datetime.datetime.now() + datetime.timedelta(minutes = -15))
print(datetime.timedelta(hours = 5))
print(datetime.datetime.now() + datetime.timedelta(days = 3))
print(datetime.datetime.now() + datetime.timedelta(days = -9))
print(datetime.datetime.now() - datetime.timedelta(days = 9))

I get the following results:

2016-06-03 16:04:03.706615
2016-06-03 15:49:03.706622
2016-06-03 15:49:03.706642
5:00:00
2016-06-06 16:04:03.706665
2016-05-25 16:04:03.706676
2016-05-25 16:04:03.706687
2016-06-03
16:04:03.706716

Solution 6 - Python

only the below code in Python 3.7 worked for me

from datetime import datetime,timedelta    
print(datetime.now()-timedelta(seconds=900))

Solution 7 - Python

Use DateTime in addition to a timedelta object http://docs.python.org/library/datetime.html

datetime.datetime.now()-datetime.timedelta(minutes=15)

Solution 8 - Python

datetime.datetime.now() - datetime.timedelta(0, 15 * 60)

timedelta is a "change in time". It takes days as the first parameter and seconds in the second parameter. 15 * 60 seconds is 15 minutes.

Solution 9 - Python

If you are using time.time() and wants timestamp as output

Simply use

CONSTANT_SECONDS = 900 # time  in seconds (900 seconds = 15 min)

current_time = int(time.time())
time_before_15_min = current_time - CONSTANT_SECONDS

You can change 900 seconds as per your required time.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWill CurranView Question on Stackoverflow
Solution 1 - PythonSpacedmanView Answer on Stackoverflow
Solution 2 - PythoneduffyView Answer on Stackoverflow
Solution 3 - Pythonuser6516765View Answer on Stackoverflow
Solution 4 - PythonTauquirView Answer on Stackoverflow
Solution 5 - PythonMona JalalView Answer on Stackoverflow
Solution 6 - PythonAnandkumarView Answer on Stackoverflow
Solution 7 - PythonHut8View Answer on Stackoverflow
Solution 8 - PythonDonald MinerView Answer on Stackoverflow
Solution 9 - Pythonvivek dhamechaView Answer on Stackoverflow