计算Javascript中两个日期之间的工作日,假期除外

IT技术 javascript jquery
2021-02-25 11:34:44

我有一个 javascript 函数,它计算两个日期之间的工作日,它可以工作,但问题是它不考虑假期。如何修改此函数,例如通过在异常数组中添加假期?

在互联网上搜索了这个问题,但没有找到假期例外。

例如假期数组:

var holidays = ['2016-05-03','2016-05-05'];

我有一个函数来计算这个:

function workingDaysBetweenDates(d0, d1) {
    var startDate = parseDate(d0);
    var endDate = parseDate(d1);  
    // Validate input
    if (endDate < startDate)
        return 0;

    // Calculate days between dates
    var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
    startDate.setHours(0,0,0,1);  // Start just after midnight
    endDate.setHours(23,59,59,999);  // End just before midnight
    var diff = endDate - startDate;  // Milliseconds between datetime objects    
    var days = Math.ceil(diff / millisecondsPerDay);

    // Subtract two weekend days for every week in between
    var weeks = Math.floor(days / 7);
    days = days - (weeks * 2);

    // Handle special cases
    var startDay = startDate.getDay();
    var endDay = endDate.getDay();

    // Remove weekend not previously removed.   
    if (startDay - endDay > 1)         
        days = days - 2;      

    // Remove start day if span starts on Sunday but ends before Saturday
    if (startDay == 0 && endDay != 6)
        days = days - 1  

    // Remove end day if span ends on Saturday but starts after Sunday
    if (endDay == 6 && startDay != 0)
        days = days - 1  

    return days;
}
function parseDate(input) {
    // Transform date from text to date
  var parts = input.match(/(\d+)/g);
  // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
  return new Date(parts[0], parts[1]-1, parts[2]); // months are 0-based
}

在 jsfiddle 中做了一个例子:

JSFiddle 示例

也许还有一些其他功能可以在 Jquery 中轻松使用?

6个回答

尝试:

var startDate = new Date('05/03/2016');
var endDate = new Date('05/10/2016');
var numOfDates = getBusinessDatesCount(startDate,endDate);

function getBusinessDatesCount(startDate, endDate) {
    let count = 0;
    const curDate = new Date(startDate.getTime());
    while (curDate <= endDate) {
        const dayOfWeek = curDate.getDay();
        if(dayOfWeek !== 0 && dayOfWeek !== 6) count++;
        curDate.setDate(curDate.getDate() + 1);
    }
    alert(count);
    return count;
}

但是我如何整合假期计数?
2021-04-20 11:34:44
这对我有用,但 startDate 在函数中被修改。我通过执行以下操作解决了这个问题:var curDate = new Date(startDate.getTime());
2021-04-26 11:34:44
我知道@dhara-parmar 的回答并没有完全回答最初的问题(因为它没有考虑假期),但作为一个快速简便的计算工作日的实用程序,这正是我所需要的。
2021-05-11 11:34:44

实现它的最简单方法是在开始日期和结束日期之间寻找这些天数。

编辑:我添加了一个额外的验证,以确保只从holidays数组中减去工作日

$(document).ready(() => {
  $('#calc').click(() => {
  var d1 = $('#d1').val();
  var d2 = $('#d2').val();
    $('#dif').text(workingDaysBetweenDates(d1,d2));
  });
});

let workingDaysBetweenDates = (d0, d1) => {
  /* Two working days and an sunday (not working day) */
  var holidays = ['2016-05-03', '2016-05-05', '2016-05-07'];
  var startDate = parseDate(d0);
  var endDate = parseDate(d1);  

// Validate input
  if (endDate <= startDate) {
    return 0;
  }

// Calculate days between dates
  var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
  startDate.setHours(0, 0, 0, 1);  // Start just after midnight
  endDate.setHours(23, 59, 59, 999);  // End just before midnight
  var diff = endDate - startDate;  // Milliseconds between datetime objects    
  var days = Math.ceil(diff / millisecondsPerDay);

  // Subtract two weekend days for every week in between
  var weeks = Math.floor(days / 7);
  days -= weeks * 2;

  // Handle special cases
  var startDay = startDate.getDay();
  var endDay = endDate.getDay();
    
  // Remove weekend not previously removed.   
  if (startDay - endDay > 1) {
    days -= 2;
  }
  // Remove start day if span starts on Sunday but ends before Saturday
  if (startDay == 0 && endDay != 6) {
    days--;  
  }
  // Remove end day if span ends on Saturday but starts after Sunday
  if (endDay == 6 && startDay != 0) {
    days--;
  }
  /* Here is the code */
  holidays.forEach(day => {
    if ((day >= d0) && (day <= d1)) {
      /* If it is not saturday (6) or sunday (0), substract it */
      if ((parseDate(day).getDay() % 6) != 0) {
        days--;
      }
    }
  });
  return days;
}
           
function parseDate(input) {
    // Transform date from text to date
  var parts = input.match(/(\d+)/g);
  // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
  return new Date(parts[0], parts[1]-1, parts[2]); // months are 0-based
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="d1" value="2016-05-02"><br>
<input type="text" id="d2" value="2016-05-08">

<p>Working days count: <span id="dif"></span></p>
<button id="calc">Calc</button>

<p>
Now it shows 5 days, but I need for example add holidays 
3 and 5 May (2016-05-03 and 2016-05-05) so the result will be 3 working days
</p>

抱歉,我尝试重用函数参数但我不能,所以我$('#d1').val()改用parseDate(d0).
2021-05-01 11:34:44
这是完美的,除了当两个日期相同时它给我 1 而不是 0 所以我从末尾减去一个
2021-05-02 11:34:44
至于一天,同样的两天问题不需要减去,只需添加: if (endDate <= startDate) { return 0; }--> if (endDate < startDate) { return 0; } else if (endDate == startDate) { return 1; }这不会检查假期和周末,但无论如何谁想在上面添加一天?
2021-05-03 11:34:44
不客气,我改进它也使用days--代替days = days - 1我不知道为什么当我做同样的更改时,代码显示我“5 天”:) 编辑:啊!parseDate 返回一个日期,而不是一个字符串!:D
2021-05-16 11:34:44
谢谢!我对您的回答进行了更改。现在一切正常:)
2021-05-17 11:34:44

我对@OscarGarcia 采取了类似的方法,主要是作为练习,因为我的 JS 生rust了。

虽然看起来很相似,但如果假期恰好在周六或周日,请注意不要将一天减去两次。这样,您可以预先加载重复日期列表(例如 12 月 25 日、1 月 1 日、7 月 4 日,这些日期可能是也可能不是工作日 - 周一至周五 -)

$(document).ready(function(){
    $('#calc').click(function(){
  var d1 = $('#d1').val();
  var d2 = $('#d2').val();
        $('#dif').text(workingDaysBetweenDates(d1,d2));
    });
});
function workingDaysBetweenDates(d0, d1) {
    var startDate = parseDate(d0);
    var endDate = parseDate(d1);
    // populate the holidays array with all required dates without first taking care of what day of the week they happen
    var holidays = ['2018-12-09', '2018-12-10', '2018-12-24', '2018-12-31'];
    // Validate input
    if (endDate < startDate)
        return 0;

    var z = 0; // number of days to substract at the very end
    for (i = 0; i < holidays.length; i++)
    {
        var cand = parseDate(holidays[i]);
        var candDay = cand.getDay();

      if (cand >= startDate && cand <= endDate && candDay != 0 && candDay != 6)
      {
        // we'll only substract the date if it is between the start or end dates AND it isn't already a saturday or sunday
        z++;
      }

    }
    // Calculate days between dates
    var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
    startDate.setHours(0,0,0,1);  // Start just after midnight
    endDate.setHours(23,59,59,999);  // End just before midnight
    var diff = endDate - startDate;  // Milliseconds between datetime objects    
    var days = Math.ceil(diff / millisecondsPerDay);

    // Subtract two weekend days for every week in between
    var weeks = Math.floor(days / 7);
    days = days - (weeks * 2);

    // Handle special cases
    var startDay = startDate.getDay();
    var endDay = endDate.getDay();

    // Remove weekend not previously removed.   
    if (startDay - endDay > 1)         
        days = days - 2;      

    // Remove start day if span starts on Sunday but ends before Saturday
    if (startDay == 0 && endDay != 6)
        days = days - 1  

    // Remove end day if span ends on Saturday but starts after Sunday
    if (endDay == 6 && startDay != 0)
        days = days - 1  

    // substract the holiday dates from the original calculation and return to the DOM
    return days - z;
}
function parseDate(input) {
    // Transform date from text to date
  var parts = input.match(/(\d+)/g);
  // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
  return new Date(parts[0], parts[1]-1, parts[2]); // months are 0-based
}

2018-12-09 是一个星期天......使用这个代码,它只会被减去一次(因为是星期天)而不是两次(就像我们只检查它是否是国定假日一样)

感谢您的增强(+1)。我用 实现它(parseDate(holidays[i]).getDay() % 6) != 0,仅在必要时检查它(所选日期之间的日期)。
2021-04-30 11:34:44

你也可以试试这段代码:

const moment = require('moment-business-days');
/**
 *
 * @param {String} date - iso Date
 * @returns {Number} difference between now and @param date
 */
const calculateDaysLeft = date => {
  try {
     return moment(date).businessDiff(moment(new Date()))
  } catch (err) {
     throw new Error(err)
  }
}
这对我来说似乎是最好的解决方案,因为它允许为语言环境配置假期。
2021-04-23 11:34:44

最佳答案实际上有效,但有一个缺陷。
当圣日在星期六或星期日时,它仍然减少一天。

将此添加到现有代码中:

.... /* Here is the code */
for (var i in holidays) {
  if ((holidays[i] >= d0) && (holidays[i] <= d1)) {

    // Check if specific holyday is Saturday or Sunday
      var yourDate = new Date(holidays[i]);
      if(yourDate.getDay() === 6 || yourDate.getDay() === 0){

          // If it is.. do nothing

      } else {

          // if it is not, reduce a day..
          days--;
      }
  }
}
它不是 php 语法或代码,而是 javascript。余数运算符 ( %)Date.getDay()方法都是 javascript。
2021-04-22 11:34:44
当然,你也可以这样做。我这样写这段代码是因为不熟悉php语法的人很容易理解
2021-05-06 11:34:44
我的错误 xD 我在评论它时正在使用 php。没有注意到我写的是 php 而不是 javascript xD
2021-05-09 11:34:44
感谢您的评论和代码(+1)。我用 实现它(parseDate(holidays[i]).getDay() % 6) != 0,仅在必要时检查它(所选日期之间的日期)。
2021-05-13 11:34:44