无法将时间添加到当前时间

时间:2013-05-31 08:49:15

标签: python python-datetime

start_time=datetime.time(9,00)
new_time=starttime+datetime(0,50) # I want new time as 9.50

但是这给我一个错误:

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

2 个答案:

答案 0 :(得分:3)

请尝试下面的python脚本:

from datetime import datetime
from datetime import timedelta 
time = datetime(2013, 5, 31, 03, 00, 00)###or you can use time = datetime.datetime.now() 

diff = timedelta(seconds = 60)

final_time = time + diff
print final_time

答案 1 :(得分:-1)

我们可以重载操作员,执行上述功能,请检查以下代码

class Mytime(datetime.time):
    def __add__(self,b):
        res = (self.hour*60+self.minute)+(b.hour*60+b.minute)
        return Mytime(res/60,res%60)

start_time = Mytime(9,0)
next_time = start_time+Mytime(9,0)