[英]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.