I need to create a DateTime object that represents the current time minus 15 minutes.
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
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.
only the below code in Python 3.7 worked for me
from datetime import datetime,timedelta
print(datetime.now()-timedelta(seconds=900))
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.
Use DateTime in addition to a timedelta
object
http://docs.python.org/library/datetime.html
datetime.datetime.now()-datetime.timedelta(minutes=15)
datetime.datetime.now() - datetime.timedelta(minutes=15)