我想为日期添加时间。日期和时间是字符串12/28/2018 23:30:00现在在这里我想添加时间02:30:00在输出中:12/29/2018 02:30我尝试了以下方法:import datetimefrom datetime import datetime, timedeltadeparture_time = "15:30"duration = "00:00:50"#I convert the strings to datetime objdeparture_time_obj = datetime.strptime(departure_time_obj, '%H:%M')duration_obj = datetime.strptime(duration_obj, '%H:%M:%S')arrival_time = dtt + datetime.timedelta(duration_obj)print(arrival_time)我收到以下错误:AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
1 回答
隔江千里
TA贡献1906条经验 获得超10个赞
用 timedelta(hours=duration_obj.hour, minutes=duration_obj.minute, seconds=duration_obj.second)
前任:
from datetime import datetime, timedelta
departure_time = "15:30"
duration = "00:00:50"
#I convert the strings to datetime obj
departure_time_obj = datetime.strptime(departure_time, '%H:%M')
duration_obj = datetime.strptime(duration, '%H:%M:%S')
arrival_time = departure_time_obj + timedelta(hours=duration_obj.hour, minutes=duration_obj.minute, seconds=duration_obj.second)
print(arrival_time)
注意:您已经导入timedelta
添加回答
举报
0/150
提交
取消