我正在尝试计算时间段长度,但是由于一个日期是timedelta
,而另一个日期是datedelta
,则会引发此错误:Unsupported operand type(s) for /: 'datetime.timedelta' and 'datedelta'
码:
from datetime import timedelta
from datedelta import datedelta
import math
step_period = datedelta(months=1)
to_from = timedelta(weeks=6)
number_of_steps = math.ceil((to_from) / step_period)
注意:
datedelta
是Python库:可以通过sudo pip3 install datedelta
获得可能的输入:
我要考虑的时间是6周(
to_from
),我希望将其分为1个月(number_of_steps
)的大块(step_period
)。会有多少个师?预期产量:
number_of_steps = 2
我该如何解决?
最佳答案
您需要转换为相同的单位,然后进行除法:
(已安装datedelta
软件包)
from datetime import timedelta
from datedelta import datedelta
import math
def delta_to_days(delta):
return delta.days + delta.months * 30
step_period = datedelta(months=1)
to_from = timedelta(days=3)
number_of_steps = math.ceil(delta_to_days(step_period) / to_from.days )
print(number_of_steps)
输出:
10
关于python -/不支持的操作数类型:'datetime.timedelta'和'datedelta',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55588854/