如何动态计算每个月的工作日?

时间:2014-03-27 07:11:12

标签: php jquery mysql

我正在使用Php开发考勤系统。

我必须计算除假期外每个月的总办公室工作日。

如果有人休假,则表示工作日的工作日应为办公室工作日的减1。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

请尝试一下,我认为它会对你有所帮助。

var get_working_days  = function(year, month_number){
  var start_dt = new Date(year, month_number, 1);
  var end_dt = null;
  var count_sunday = 0;

  /* these are moonth number on based of index like 0 for jan, 1 for feb etc*/
  var month_31 = [0,2,4,6,7,9,11];
  var month_30 = [3,5,8,10];
  var month_feb = [1];

  if(month_31.indexOf(month_number) >= 0){
    end_dt = new Date(year, month_number, 31);
  }else if(month_30.indexOf(month_number) >= 0){
    end_dt = new Date(year, month_number, 30);
  }else if(month_feb.indexOf(month_number) >= 0){
    if(year%4 == 0){
      end_dt = new Date(year, month_number, 29);
    }else{
      end_dt = new Date(year, month_number, 28);
    }
  }
  var number_of_days = end_dt.getDate();
  while(number_of_days >= 7){
    count_sunday += 1;
    number_of_days = number_of_days - 7;
  }
  if(number_of_days > 0){
    if(start_dt.getDay()+number_of_days >= 7){
      count_sunday += 1;    
    }
  }
  var working_days = end_dt.getDate() - count_sunday;
  return working_days;
}

var dt = new Date();
var year = dt.getFullYear();
var month_lst = ['jan', 'feb', 'mar', 'apr', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ]
var month = dt.getMonth()+1; /*pass number 1 ,2 3 ... for month on the based of indes   like 0 for january*/
for(var count = 0 ; count < month_lst.length; count++){
  var working_days = get_working_days(year, count)
  console.log(" Working days of "+month_lst[count] +" " + year +" are : "+ working_days);
}

请查看图片以获得更多理解。

enter image description here