2013-11-14 46 views
1

我有兩個字符串deltatime。我要添加兩個deltatime。 Python中不支持操作數「+」。有什麼想法嗎?如何在python中的兩個deltatime之間添加

delA = "00:45:34.563" 

delB = "00:25:24.266" 

a = datetime.datetime.strptime(delA, "%H:%M:%S.%f") 

b = datetime.datetime.strptime(delB, "%H:%M:%S.%f") 

print a, b 
1900-01-01 00:45:34.563000 1900-01-01 00:25:24.266000 

print a-b 
0:20:10.297000 

print a+b 
--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-43-31453f7268bc> in <module>() 
----> 1 print a+b 

TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'datetime.datetime' 
+2

你期待的結果是什麼? –

+0

預計:「01:10:58.829」 –

回答

2

你可以添加timedeltadatetime。也許你可以這樣做:

>>> b_timedelta = datetime.timedelta(hours=b.hour, minutes=b.minute, seconds=b.second, microseconds=b.microsecond) 
>>> result = a + b_timedelta 
>>> print result 
datetime.datetime(1900, 1, 1, 1, 10, 58, 829000) 
>>> print result.strftime('%H:%M:%S.%f') 
'01:10:58.829000' 
相關問題