I need to create a DateTime object that represents the current time minus 15 minutes.
Asked
Active
Viewed 1.8e+01k times
162
-
Related question: http://stackoverflow.com/questions/100210/python-easy-way-to-add-n-seconds-to-a-datetime-time/100345#100345 – spade78 Dec 27 '10 at 20:43
9 Answers
284
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)

Spacedman
- 92,590
- 12
- 140
- 224
-
2NB, in Python 3 you'll need to pass the timezone to `now()` to avoid an error about subtracting offset-naive and offset-aware datetimes: `datetime.datetime.now(datetime.timezone.utc)` – nornagon Dec 21 '17 at 20:18
-
2@nornagon that’s not at all applicable here; it doesn’t matter if the datetime object is aware or naive, subtracting a timedelta works regardless. – Martijn Pieters Jan 29 '20 at 00:32
11
import datetime
datetime.datetime.now() - datetime.timedelta(0, 900)
Actually 900 is in seconds. Which is equal to 15 minutes. `15*60 = 900`
-
Your import doesn’t match the code; you import `timedelta` but then use `datetime` attributes. Either import the module, or add the type. – Martijn Pieters Jan 29 '20 at 00:30
9
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

Mona Jalal
- 34,860
- 64
- 239
- 408
7
Use DateTime in addition to a timedelta
object
http://docs.python.org/library/datetime.html
datetime.datetime.now()-datetime.timedelta(minutes=15)

Hut8
- 6,080
- 4
- 42
- 59
7
only the below code in Python 3.7 worked for me
from datetime import datetime,timedelta
print(datetime.now()-timedelta(seconds=900))

Anandkumar
- 1,338
- 13
- 15
4
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.

Donald Miner
- 38,889
- 8
- 95
- 118
2
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.

vivek dhamecha
- 409
- 3
- 14