0

我有 2 个输入,其中选择了日期 #startdate 和 #enddate。我正在使用 datepicker,目前禁用了周末和节假日,这很好用!

然后我有一个#days 输入,它计算两个日期之间的差异,然后这个日期差异有一个周末计数。

我想创建一个假期计数,这样我也可以从天数差中减去这个。

所以我最终会得到

$('#days').val((Math.abs(($d2new-$d1new)/86400000) - weekend_count - holidaycount) + 1);

我当前的代码如下(这是从其他 stackoverflow 问题中提取的,并且运行良好:)底部是我正在努力解决的 var holidaycount。任何帮助将不胜感激。

    //holidays
    var natDays = [
      [1, 1, 'uk'],
      [1, 2, 'uk'],
      [1, 3, 'uk'],
      [1, 4, 'uk'],
      [12, 24, 'uk'],
      [12, 25, 'uk'],
      [12, 26, 'uk'],
      [12, 27, 'uk'],
      [12, 28, 'uk'],
      [12, 29, 'uk'],
      [12, 30, 'uk'],
      [12, 31, 'uk']
    ];


    function noWeekendsOrHolidays(date) {
        var noWeekend = $.datepicker.noWeekends(date);
        if (noWeekend[0]) {
            return nationalDays(date);
        } else {
            return noWeekend;
        }
    }
    function nationalDays(date) {
        for (i = 0; i < natDays.length; i++) {
            if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
                return [false, natDays[i][2] + '_day'];
            }
        }
        return [true, ''];
    }



// do initialization here
$("#startdate").datepicker({
            dateFormat: 'dd-mm-yy',
            changeMonth: true,
            changeYear: true,
            firstDay: 1,
            yearRange: '0:+100',
            beforeShowDay: noWeekendsOrHolidays,
            onSelect: function( selectedDate ) {
                $("#enddate").datepicker("option","minDate",selectedDate );
                $("#enddate2").datepicker("option","minDate",selectedDate );
            },              
            minDate: '+1d',         
            maxDate: '+' + DAY_DIFFERENCE + 'd'
});

// do initialization here
$("#enddate").datepicker({
            dateFormat: 'dd-mm-yy',
            changeMonth: true,
            changeYear: true,
            firstDay: 1,
            yearRange: '0:+100',
            beforeShowDay: noWeekendsOrHolidays,
            maxDate: '+' + DAY_DIFFERENCE + 'd'
});

// do initialization here
$("#enddate2").datepicker({
            dateFormat: 'dd-mm-yy',
            changeMonth: true,
            changeYear: true,
            firstDay: 1,
            yearRange: '0:+100',
            beforeShowDay: noWeekendsOrHolidays,
            onSelect: function( selectedDate ) {
                $d1 = $('#startdate').val();
                $d2 = $('#enddate2').val();

                $myDateParts1 = $d1.split("-");
                $myDateParts2 = $d2.split("-");

                $d1flip = new Date($myDateParts1[2], ($myDateParts1[1]-1), $myDateParts1[0]);
                $d2flip = new Date($myDateParts2[2], ($myDateParts2[1]-1), $myDateParts2[0]);

                $newdate1 = $d1flip.format("ddd mmm dd hh:MM:ss yyyy");
                $newdate2 = $d2flip.format("ddd mmm dd hh:MM:ss yyyy");

                // For Opera and older winXP IE n such              
                $d1new  = Date.parse($newdate1);
                $d2new  = Date.parse($newdate2);

                var weekend_count = 0;
                    for (i = $d1new.valueOf(); i <= $d2new.valueOf(); i+= 86400000){
                    var temp = new Date(i);
                        if (temp.getDay() == 0 || temp.getDay() == 6) {
                            weekend_count++;
                        }
                }

                var holidaycount = 0;
                    for (i = $d1new.valueOf(); i <= $d2new.valueOf(); i+= 86400000){
                    var temp = new Date(i);
                        if (**date in var natDays & is in selected difference range**) {
                            holidaycount++;
                        }
                }


            console.log(weekend_count);
            console.log(holidaycount);

                $('#days').val((Math.abs(($d2new-$d1new)/86400000) - weekend_count - holidaycount) + 1);
            }           
});


}, 'html');
return false;
});
}

编辑

从答案中,我插入了函数并尝试了这个.... console.log(holidaycount); 返回 0 但我选择的日期应该是 10

            var holidaycount = 0;
                for (i = $d1new.valueOf(); i <= $d2new.valueOf(); i+= 86400000){
                var temp = new Date(i);
                    if (isHoliday(temp)) {
                        holidaycount++;
                }                       
                }
4

1 回答 1

1

更新:根据评论getMonth()进行相应修改。getMonth() + 1

**date in var natDays & is in selected difference range**

日期在选定的差异范围内始终为真,就像new Date(i)在所述范围内一样(就像在循环中一样weekend_count:您不必费心检查您是否在该范围内)。

现在,检查里面的日期temp是否是假期,您可能想要使用一个函数(尽管您仍然可以直接在代码中执行此操作):

/* dateObject is a JS Date object (e.g. dateObject = new Date();) */
/* country is the country we test holidays for */
function isHoliday(dateObject, country) {
  /* let's assume natDays is global, otherwise pass it as a third argument to this function */
  for(var i = 0; i < natDays.length; i++) {
    /* natDays[i][0] is a day, natDays[i][1] is a month, natDays[i][2] is a country indicator */
    if(parseInt(dateObject.getDate()) == parseInt(natDays[i][0]) && parseInt(dateObject.getMonth()) + 1 == parseInt(natDays[i][1]) && country.toLowerCase() == natDays[i][2].toLowerCase()) {
      /* found a day and a month matching our current Date's day and month: interrupt and tell caller dateObject is a holiday */
      return true;
    }
  }
  /* end of loop, we parsed all possible holidays without finding any match for our Date: tell caller the given Date is not a holiday */
  return false;
}

现在把它放在你的代码中:

if (isHoliday(temp, 'uk')) {
  holidaycount++;
}

这应该可以解决问题,尽管它可能需要一些重构(没有测试此代码),并且可能有更优雅的方法来做到这一点(例如修改 Date 对象的原型以将此函数用作对象的方法)。

于 2012-09-21T11:59:31.257 回答