Datepicker startdate / enddate的区别排除了周末和填充输入

时间:2012-09-14 15:08:46

标签: jquery forms jquery-plugins datepicker jquery-ui-datepicker

我正在尝试计算用户使用datepicker选择的startdate和enddate之间的天数(不包括周末)的差异。

然后希望将此天数输入#days输入。 有谁知道我哪里出错了?

// do initialization here
$("#startdate").datepicker({
            dateFormat: 'dd-mm-yy',
            changeMonth: true,
            changeYear: true,
            firstDay: 1,
            yearRange: '0:+100',
            beforeShowDay: $.datepicker.noWeekends,
            onSelect: function( selectedDate ) {
var startdate = $("#startdate").val();
var enddate =   $("#enddate").val();                
var weekend_count = 0;
                        for (i = startdate.valueOf(); i <= enddate.valueOf(); i+= 86400000){
                        var temp = new Date(i);
                            if (temp.getDay() == 0 || temp.getDay() == 6) {
                                weekend_count++;
                            }
                    }
                    $("#days").val((startdate - enddate) / 86400000 - weekend_count);
                    $( "#enddate" ).datepicker( "option", "minDate", selectedDate );
                },              
                minDate: '+1d',         
                maxDate: '+' + (DAY_DIFFERENCE + 1) + 'd'
    });



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

编辑 - 我自己到那里,有没有人有任何意见?以下解决方案有点工作(但我正在与php变量挣扎 - 如何在php日期函数中包含var)。如果是Opera和旧的XP机器,则为isNAN。

    //holidays
    var natDays = [
      [1, 1, 'uk'],
      [12, 25, 'uk'],
      [12, 26, '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 );
            },              
            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,
            onSelect: function( selectedDate ) {
            $d1 = $('#startdate').val();
            $d2 = $('#enddate2').val();

            console.log($d1);
            console.log($d2);

            $d1p = Date.parse($d1)
            $d2p = Date.parse($d2)

            console.log($d1p);
            console.log($d2p);

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

            console.log($myDateParts1);
            console.log($myDateParts2);

            $myNewDate1 = new Date($myDateParts1[2], $myDateParts1[1], $myDateParts1[0]);
            $myNewDate2 = new Date($myDateParts2[2], $myDateParts2[1], $myDateParts2[0]);

            console.log($myNewDate1);
            console.log($myNewDate2);

            $d1flip= $myNewDate1; 
            $d2flip= $myNewDate2; 

            console.log($d1flip);
            console.log($d2flip);

            // For Opera and older winXP IE n such
            if (isNaN($d1p)) { 
                $d1p  = Date.parse($d1flip);
            }
            if (isNaN($d2p)) { 
                $d2p  = Date.parse($d2flip);
            }


                var weekend_count = 0;
                    for (i = $d1p.valueOf(); i <= $d2p.valueOf(); i+= 86400000){
                    var temp = new Date(i);
                        if (temp.getDay() == 0 || temp.getDay() == 6) {
                            weekend_count++;
                        }
                }
                $('#days').val((Math.abs(($d2p-$d1p)/86400000) - weekend_count));
            }           
});

0 个答案:

没有答案