在 Python 中为日期添加时间

我想为日期添加时间。日期和时间是字符串


12/28/2018 23:30:00

现在在这里我想添加时间


02:30:00

在输出中:


12/29/2018 02:30

我尝试了以下方法:


import datetime

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_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'


侃侃尔雅
浏览 297回答 1
1回答

隔江千里

用 timedelta(hours=duration_obj.hour, minutes=duration_obj.minute, seconds=duration_obj.second)前任:from datetime import datetime, timedeltadeparture_time = "15:30"duration = "00:00:50"#I convert the strings to datetime objdeparture_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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python