# 如何创建等于15分钟前的DateTime？

## 问题：如何创建等于15分钟前的DateTime？

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

## 回答 0

``````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)``````

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)
``````

## 回答 1

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

## 回答 2

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

`timedelta`s是专门设计用来允许您减去`datetime`s 或增加s （差）。

This is simply what to do:

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

`timedelta`s are specifically designed to allow you to subtract or add deltas (differences) to `datetime`s.

## 回答 3

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

Actually 900 is in seconds. Which is equal to 15 minutes. `15*60 = 900```````
``````from datetime import timedelta
datetime.datetime.now() - datetime.timedelta(0, 900)

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

## 回答 4

``````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))``````

``````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``````

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
``````

## 回答 5

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

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

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

## 回答 6

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

`timedelta`是“时间的变化”。以天为第一个参数，以秒为第二个参数。`15 * 60`秒是15分钟。

`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.

## 回答 7

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

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

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.

## 回答 8

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

only the below code in Python 3.7 worked for me

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