如何使timedelta成为字符串

时间:2015-08-03 14:57:22

标签: python time strftime timedelta

即时通讯使用T1-T2以获取时间增量,然后我想将其恢复为字符串 我正在尝试以下代码

layover_info = datetime.strptime(leg_info['dep_time'], '%H:%M') - start_waiting_time
layover_info_str = layover_info.strftime('%H:%M')

但它不起作用..我得到错误

error: 'datetime.timedelta' object has no attribute 'strftime'

1 个答案:

答案 0 :(得分:0)

您可以手动执行此操作:

def my_strftime(layover_info):
   hours, remainder = divmod(layover_info.seconds, 3600)
   minutes, seconds = divmod(remainder, 60)
   print '%02s:%02s' % (hours, minutes)

layover_info = datetime.strptime(leg_info['dep_time'], '%H:%M') -     start_waiting_time
layover_info_str = my_strftime(layover_info)