0
cdr_start_time = "12:10:13"
start_time = "00:10:00"

if cdr_start_time > start_time:
    start_time = start_time + 10

在上面的代码中,我想在时间比较后添加 10 分钟,在 python 中我该怎么做。先感谢您

4

3 回答 3

5

使用datetime模块:

import datetime
cdr_start_time = "12:10:13"
start_time = "00:10:00"
mydate1 = datetime.datetime.strptime(cdr_start_time, '%H:%M:%S') # Creates a datetime object
mydate2 = datetime.datetime.strptime(start_time, '%H:%M:%S')
if mydate1 > mydate2:
    mydate2 += datetime.timedelta(minutes=10) # Adds ten minutes to the datetime object

print datetime.datetime.strftime(mydate2, '%H:%M:%S') # Turns it back to a readable string

印刷:

00:20:00
于 2013-08-25T06:53:36.123 回答
0

pandas你可以用Timestamp类来做到这一点,它是一个子类datetime.datetime

In [63]: from pandas import Timestamp

In [64]: from pandas.offsets import Minute, date_range

In [65]: pd.Timestamp('1/1/2001') + Minute(10)
Out[65]: Timestamp('2001-01-01 00:10:00', tz=None)

这也扩展到datetime64s 的数组:

In [66]: date_range('1/1/2001', periods=10).to_series() + Minute(10)

Out[66]:
2001-01-01   2001-01-01 00:10:00
2001-01-02   2001-01-02 00:10:00
2001-01-03   2001-01-03 00:10:00
2001-01-04   2001-01-04 00:10:00
2001-01-05   2001-01-05 00:10:00
2001-01-06   2001-01-06 00:10:00
2001-01-07   2001-01-07 00:10:00
2001-01-08   2001-01-08 00:10:00
2001-01-09   2001-01-09 00:10:00
2001-01-10   2001-01-10 00:10:00
Freq: D, dtype: datetime64[ns]
于 2013-08-25T07:04:37.493 回答
0

您可以使用时间增量

from datetime import datetme,timedelta
>>> d = datetime.now()
datetime.datetime(2013, 8, 25, 12, 23, 14, 410952)

>>> d1 = d+timedelta(minutes=20)
datetime.datetime(2013, 8, 25, 12, 23, 14, 410952)
于 2013-08-25T06:56:20.740 回答