[英]Unsupported operand type(s) for /: 'datetime.timedelta' and 'datedelta'
I'm trying to calculate a time period length, but as one date is a timedelta
and the other is a datedelta
, this error is thrown: 我正在尝试计算一个时间段长度,但由于一个日期是timedelta
而另一个是datedelta
,因此抛出此错误:
Unsupported operand type(s) for /: 'datetime.timedelta' and 'datedelta'
Code: 码:
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)
NB: datedelta
is a Python library: it can be obtained wih sudo pip3 install datedelta
注意: datedelta
是一个Python库:它可以通过sudo pip3 install datedelta
Possible input: 可能的输入:
My length of time to consider is 6 weeks ( to_from
), I wish to divide it into chunks ( number_of_steps
) of 1 month ( step_period
). 我需要考虑的时间是6周( to_from
),我希望将其分成1个月( step_period
)的块( number_of_steps
)。 How many divisions will there be? 会有多少个部门?
Expected output: number_of_steps = 2
预期输出: number_of_steps = 2
How can I solve this? 我怎么解决这个问题?
You need to convert to the same units and then do a division: (having installed datedelta
package) 您需要转换为相同的单位,然后进行划分:(已安装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)
Output: 输出:
10
I'm pretty sure timedelta won't allow for months= as an input. 我很确定timedelta不会允许数月=作为输入。 I also could not find this datedelta you have. 我也找不到你的日期。 Instead I could replicate your answer as per: 相反,我可以按照以下方式复制你的答案:
In: math.ceil(timedelta(weeks=6)/timedelta(weeks=4))
Out: 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.