Python:无法添加分钟以腾出时间

时间:2018-09-27 11:22:37

标签: python python-3.x datetime

我正在尝试将分钟添加到另一个时间戳。我的代码如下所示

from datetime import datetime
from datetime import timedelta 

start_time = '06:52:00'
easy_pace_time = '00:08:15'
tempo_pace_time = '00:07:12'
easy_pace_miles = 2
tempo_pace_miles = 3 
FMT = '%H:%M:%S'

easy_strip = datetime.strptime( easy_pace_time, FMT)
easy_strip_seconds = (easy_strip.minute * 60) + easy_strip.second
easy_pace_total = easy_pace_miles * easy_strip_seconds

tempo_strip = datetime.strptime( tempo_pace_time, FMT)
tempo_strip_seconds = ( tempo_strip.minute * 60 ) + tempo_strip.second
tempo_pace_toal = tempo_pace_miles * tempo_strip_seconds

total_running_time = (( easy_pace_total + tempo_pace_toal ) / 60 )
print(total_running_time)

print(str ( timedelta(minutes=total_running_time) ))

# Tried this as well but failed with as directly two timestamps can not be added in Python
#return_time = datetime.strptime(start_time, FMT) + datetime.strptime( str ( timedelta(minutes=total_running_time) ), FMT)
#print(return_time)

datetime.strptime(start_time, FMT) + datetime.timedelta( minutes = total_running_time)

在执行此操作时,我收到错误消息

Traceback (most recent call last):
  File "C:/Users//Dropbox/Python_code/ThinkPython/timpo_pace_cal.py", line 30, in <module>
    datetime.strptime(start_time, FMT) + datetime.timedelta( minutes = total_running_time)
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'

1 个答案:

答案 0 :(得分:2)

更改

datetime.strptime(start_time, FMT) + datetime.timedelta( minutes = total_running_time)

datetime.strptime(start_time, FMT) + timedelta( minutes = total_running_time)