Javascript连续工作日(无周末)

时间:2017-03-01 20:16:14

标签: javascript jquery

我需要连续3个工作日检查javascript中的日期数组。我有任何3天的工作,但我无法弄清楚我的生活如何让这个工作与周末分手连续几天。

注意 - 这需要时刻()库(https://momentjs.com

我的代码连续3天有效:

var allDates = ["2017-03-07", "2017-03-09", "2017-03-10", "2017-03-13", "2017-03-15"];
var diff = 86400000;
var consecutive = 0;

allDates.sort(function(a,b){
  return new Date(a) - new Date(b);
});

for (i = 2; i < allDates.length; i++) {
  var d = moment(allDates[i], "YYYY-MM-DD").format('x');
  var d1 = moment(allDates[i-1], "YYYY-MM-DD").format('x');
  var d2 = moment(allDates[i-2], "YYYY-MM-DD").format('x');

  if (d1 - d2 == diff && d - d2 == diff * 2) {
    consecutive = 1;               
    break;
  }
}

enter image description here

正如您在上面的日历中所看到的,我希望3月9日,10日和13日连续3个工作日。任何人都有任何反馈可以指导我走上正确的道路吗?

提前致谢

3 个答案:

答案 0 :(得分:1)

您可以使用javascript Date对象来确定哪些天是周末。 这是documentation

一个简单的例子:

    var d1 = new Date("2017-03-07");
    var d2 = new Date("2017-03-08");
    d.getDay() // this will return 1, which corresponds with Monday. 2  is Tuesday, 3 is Wednesday, etc.

这是一个小函数来判断2个日期是否连续,即使是周末分开,也可以是:

     var millisecondsInDay = 86400000;

     function datesAreConsecutiveBusinessDays(date1, date2){
       // If date1 is a Friday and date2 is a Monday
       if (date1.getDay() === 5 && date2.getDay() === 1){
         // And the dates are 2 days apart
         date2.valeOf() - date1.valueOf() === millisecondsInDay * 2
         return true;
       } else if (date2.valeOf() - date1.valueOf() === millisecondsInDay){
         return true;
       } else {
         return false;
       }
     }

因此,使用此方法,您可以查看日期列表,并在彼此相邻的每两个日期上调用它。

     var consecutive = 0;
     var allDates = ["2017-03-07", "2017-03-09", "2017-03-10", "2017-03-13", "2017-03-15"];

     for (var i = 0; i < allDates.length - 1; i++){
       if (datesAreConsecutiveBusinessDays(new Date(allDates[i]), new Date(allDates[i+1])){
         consecutive++;
       }
       // If you want to start over if you hit a nonconsecutive date, add this else:
       else {
         consecutive = 0; // this will start the counter over
       }
     }

根据计算连续性的方式,您可能需要稍微调整一下解决方案,但这应该足以让您入门。

答案 1 :(得分:1)

这可以通过编写一个函数来解决,该函数确定任何2个momentjs实例是否为1天不同,或者如果一个是星期五而另一个是星期一(但必须相隔3天)。简单地说,就是这样:

function isConsecutive(a,b){
    return b.diff(a,"days") == 1
        || (a.weekday() == 5 && b.weekday() == 1 && b.diff(a,"days") == 3);
}

然后,您可以获取日期数组,将它们全部转换为momentJS实例,对它们进行排序并通过为每对调用上述方法进行迭代。保持计数的总计:

function countConsecutiveDays(arr){
   var momentDates = arr.map(function(d){
       return  moment(d, "YYYY-MM-DD") ;
   }).sort(function(a,b){return a.diff(b);})
    var count = 0;
    for(var i=1;i<momentDates.length;i++){
        if(isConsecutive(momentDates[i-1],momentDates[i]))
            count++;
    }
    return count+1;
}

下面是一个工作示例

&#13;
&#13;
function countConsecutiveDays(arr){
   var momentDates = arr.map(function(d){
       return  moment(d, "YYYY-MM-DD") ;
   }).sort(function(a,b){return a.diff(b);})
    var count = 0;
    for(var i=1;i<momentDates.length;i++){
        if(isConsecutive(momentDates[i-1],momentDates[i]))
            count++;
    }
    return count+1;
}

function isConsecutive(a,b){
    return b.diff(a,"days") == 1
        || (a.weekday() == 5 && b.weekday() == 1 && b.diff(a,"days") == 3);
}


console.log(countConsecutiveDays( ["2017-03-07", "2017-03-09", "2017-03-10", "2017-03-13", "2017-03-15"]));
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

到目前为止,我的所有测试似乎都有效...除非有人能指出问题或错误。

注意 - 这需要时刻()库(https://momentjs.com

var allDates = ["2017-03-07", "2017-03-09", "2017-03-10", "2017-03-13", "2017-03-15"];
var consecutive = 0;

allDates.sort(function(a,b){
  return new Date(a) - new Date(b);
});

for (i = 2; i < allDates.length; i++) {
  var d = moment(allDates[i], "YYYY-MM-DD");
  var d1 = moment(allDates[i-1], "YYYY-MM-DD");
  var d2 = moment(allDates[i-2], "YYYY-MM-DD");
					
  if (d1.diff(d2,"days") == 1 && d.diff(d2,"days") == 2 ||
      d2.weekday() == 5 && d1.weekday() == 1 && d1.diff(d2,"days") == 3 && d.diff(d1,"days") == 1 || 
      d1.weekday() == 5 && d.weekday() == 1 && d.diff(d1,"days") == 3 && d1.diff(d2,"days") == 1) {

    consecutive = 1;               
    break;
  }
}
        
console.log('dates are consecutive: ' + consecutive)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>