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

问题:如何创建等于15分钟前的DateTime?

我需要创建一个DateTime对象,该对象代表当前时间减去15分钟。

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


回答 0

导入datetime,然后导入神奇的timedelta内容:

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)

timedeltas是专门设计用来允许您减去datetimes 或增加s (差)。

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.


回答 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

除了timedelta对象 http://docs.python.org/library/datetime.html外,还使用DateTime

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

如果您使用的是time.time()时间戳记作为输出

只需使用

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

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

您可以根据所需时间更改900秒。

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

只有以下Python 3.7中的代码对我有用

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