[英]function to calculate days of work
大家好,我想计算两个日期之间的工作天数而不计算星期日和星期六,但我不知道该怎么做! 这是我使用的代码,但它不起作用
file.py
import datetime
import math
from datetime import date
from openerp.osv import osv, fields, orm
class obj_ghb(osv.osv):
_name = 'obj.ghb'
_description = 'objet ghb'
def get_total_days( self, cr, uid, ids,days_tota,arg, context = {}):
diff_day={}
for record in self.browse(cr, uid, ids, context=context):
s_date = datetime.datetime.strptime(record.datedebut, "%Y-%m-%d").date()
e_date =datetime.datetime.strptime(record.datefin, "%Y-%m-%d").date()
diff_day[record.id] =(e_date-s_date).days
return diff_day
_columns = {
'nomprojet': fields.char('Nom du projet'),
'responsable': fields.char('Responsable GHB'),
'client': fields.char('Client'),
'contactclient': fields.char('Contact du client'),
'datedebut': fields.date('Date de debut'),
'datefin': fields.date('Date de fin'),
'nombredejour': fields.function(get_total_days, type = "integer", method=True, store = True),
'obj_ghb_parent': fields.one2many('loyer', 'loyer_obj_ghb'),
'obj_ghb_id': fields.one2many('assurance', 'assurance_obj_ghb'),
'obj_ghb_parenttt': fields.one2many('salaire', 'salaire_obj_ghb'),
'obj_ghb_parentttt': fields.one2many('autres', 'autres_obj_ghb'),
我想你的代码是在 python 中的。 我不使用python,所以我不知道你的代码中是否有一些代码错误,但乍一看你做的是错误的。
s_date = datetime.datetime.strptime(record.datedebut, "%Y-%m-%d").date()
e_date =datetime.datetime.strptime(record.datefin, "%Y-%m-%d").date()
diff_day[record.id] =(e_date-s_date).days
您写道,您不想计算周末天数,而且您的天数差异可能是错误的。
假设您想计算 2017 年 1 月从星期一到星期五的天数。星期一是 2.1。 周五是 6.1。 如果您进行 diff_day 计算,则 e_date - s_date = 6. - 2. = 4 这显然是错误的,因为它是 5 天。
所以你的天数会像e_date - s_date + 1
。
接下来,您需要在您的时间范围内减去周六和周日的数量+您可能还想排除一些公共假期等或其他非工作日。
看看这个问题的答案: 计算两个日期之间的工作日数?
它应该引导你走向正确的道路。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.