如何在python中添加10分钟到日期时间

0 python time

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

if cdr_start_time > start_time:
    start_time = start_time + 10
Run Code Online (Sandbox Code Playgroud)

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

Ter*_*ryA 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
Run Code Online (Sandbox Code Playgroud)

打印:

00:20:00
Run Code Online (Sandbox Code Playgroud)