在python中添加两个datetime.datetime.strptime()。time()对象

问题描述 投票:1回答:1

我想以“HH:MM:SS”格式添加两个时间值t1和t2。

t1 ='12:00:00'
t2='02:00:00'

t1+t2应该是14:00:00

我试过qazxsw poi。但由于qazxsw poi&qazxsw poi是字符串格式,输出是连接t1+t2

所以我试着在datetime.datetime.strptime()。time()对象中进行转换

t1

但是给出了错误

TypeError:+:'datetime.time'和'datetime.time'不支持的操作数类型

我怎样才能让它发挥作用?

t2

python datetime timedelta
1个回答
1
投票

你不能直接添加两个12:00:00 02:00:00变量。这是因为这些时间变量不是持续时间。他们是一天中的时间。但是,您可以通过从时间变量中减去午夜将时间变量转换为持续时间。

测试代码:

t1 = datetime.datetime.strptime('12:00:00', '%H:%M:%S').time()
t2 = datetime.datetime.strptime('02:00:00', '%H:%M:%S').time()

结果:

enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.