简体   繁体   English

计算工作天数的函数

[英]function to calculate days of work

hello everyone i want to calculate the days of work between two dates without calculing sunday and saturday but i don't know how to do that !大家好,我想计算两个日期之间的工作天数而不计算星期日和星期六,但我不知道该怎么做! this is the code that i used but it doesn't 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'),

I suppose your code is in python.我想你的代码是在 python 中的。 I don't work with python, so I don't know if you have some code errors in your code, but at first sight what you do is wrong.我不使用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

You wrote you don't want to count the weekend days also your diff of days is probably wrong.您写道,您不想计算周末天数,而且您的天数差异可能是错误的。
Lets say you want to count days from monday to friday in January 2017. Monday was 2.1.假设您想计算 2017 年 1 月从星期一到星期五的天数。星期一是 2.1。 and Friday was 6.1.周五是 6.1。 If you do your diff_day calculation e_date - s_date = 6. - 2. = 4 which is obviously wrong, because its 5 days.如果您进行 diff_day 计算,则 e_date - s_date = 6. - 2. = 4 这显然是错误的,因为它是 5 天。
So your number of days count will be like e_date - s_date + 1 .所以你的天数会像e_date - s_date + 1

Next you need to subtract number of saturdays and sundays in your time range + you will probably also want to exclude some public holidays etc or other non working days.接下来,您需要在您的时间范围内减去周六和周日的数量+您可能还想排除一些公共假期等或其他非工作日。

Look at answer for this question: Calculate the number of business days between two dates?看看这个问题的答案: 计算两个日期之间的工作日数?

It should lead you to right way.它应该引导你走向正确的道路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM