如何计算两个日期时间字段之间的小时数

时间:2015-02-05 09:11:09

标签: odoo

我有两个日期时间字段

 '`date_from`': fields.datetime('Submission Date').
 '`date_to`': fields.datetime('Return Date').

所以我需要在以下字段中的两个日期之间的小时数

 '`diff_hrs`': fields.float('Total Hrs').

我怎么能得到它请帮帮我

2 个答案:

答案 0 :(得分:0)

请尝试以下操作:

添加field.function

"total_hours" : fields.function(_calculate_total_hours,string="Total Hours", type="float")

import datetime

def _calculate_total_hours(self,cr,uid,ids,Name=None,args=None,context=None):
    res={}
    for record in self.browse(cr,uid,ids,context=context):
        d_frm_obj = datetime.datetime.strptime(record.date_from, DEFAULT_SERVER_DATETIME_FORMAT)
        d_to_obj = datetime.datetime.strptime(record.date_to, DEFAULT_SERVER_DATETIME_FORMAT)

        diff = d_to_obj - d_frm_obj

        hours = (diff.seconds)/ 3600
        print hours

        diff_days = diff.days
        print diff_days

        days_hours = diff_days * 24
        print days_hours

        total_hours = days_hours + hours
        print total_hours
        res[record.id]=total_hours
    return res

答案 1 :(得分:0)

发布如果您愿意,可以帮助您better

def onchange_return_date(self, cr, uid, ids, date_from, date_to):
 a=datetime.strptime(date_to,"%Y-%m-%d")
 b=datetime.strptime(date_from,"%Y-%m-%d")
 timedelta = b - a
 diff=timedelta.days
 return {'value': {'days':diff}}