如何计算 python 中两个日期时间之间的时间?

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

我有两个日期时间如下

start=2023/04/05 08:00:00, end=2023/04/05 16:00:00

我想赶时间

lTime = start + (end - start) /3 or 
lTime = (start * 2 + end) /3 

我使用如下代码

lTime = start + timedelta(end - start) / 3

但是我出错了,我该怎么办?

unsupported operand type(s) for -: 'builtin_function_or_method' and 'datetime.datetime'
python datetime timedelta
1个回答
0
投票

你需要使用

datetime
对象来做这个计算。

from datetime import datetime, timedelta

start_str = '2023/04/05 08:00:00'
end_str = '2023/04/05 16:00:00'

start = datetime.strptime(start_str, '%Y/%m/%d %H:%M:%S')
end = datetime.strptime(end_str, '%Y/%m/%d %H:%M:%S')

lTime = start + (end - start) / 3
© www.soinside.com 2019 - 2024. All rights reserved.