gpt4 book ai didi

python - 如何区分两个日期时间字段结果的小时数,odoo?

转载 作者:行者123 更新时间:2023-12-01 04:18:42 27 4
gpt4 key购买 nike

我想对两个日期时间字段进行差异,结果以小时为单位。我没有收到任何错误,但这不起作用。任何人都可以帮忙解决这个问题吗?

@api.one
@api.onchange('end_production_date')
def onchange_production_dates(self):
begin_production_date=self.begin_production_date
end_production_date=self.end_production_date
production_duration=self.production_duration

value = {}
if not begin_production_date:
return value
if not end_production_date and not production_duration:
duration = 1.00
value[production_duration] = duration

start = datetime.strptime(begin_production_date, "%Y-%m-%d %H:%M:%S")
if end_production_date and not production_duration:
end = datetime.strptime(end_production_date, "%Y-%m-%d %H:%M:%S")
diff = end - start
duration = float(diff.days)* 24 + (float(diff.seconds) / 3600)
value[production_duration] = round(duration, 2)
elif not end_production_date:
end = start + timedelta(hours=production_duration)
value[end_production_date] = end.strftime("%Y-%m-%d %H:%M:%S")
elif end_production_date and production_duration:
end = datetime.strptime(end_production_date, "%Y-%m-%d %H:%M:%S")
diff = end - start
duration = float(diff.days)* 24 + (float(diff.seconds) / 3600)
value['production_duration'] = round(duration, 2)

return {'value': value}

谢谢

最佳答案

我会在 timedelta 对象上使用 total_seconds 方法:

total_hours = (end - start).total_seconds() / 3600

编辑:60 分钟后...

关于python - 如何区分两个日期时间字段结果的小时数,odoo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33999472/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com