I need to create a DateTime object that represents the current time minus 15 minutes.
9 Answers
264
votes
12
votes
9
votes
8
votes
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
6
votes
Use DateTime in addition to a timedelta object
http://docs.python.org/library/datetime.html
datetime.datetime.now()-datetime.timedelta(minutes=15)
5
votes
4
votes