datepickerを使用して、ユーザーが選択した開始日と終了日の日数(週末を除く)の違いを調べようとしています。
この日数は、#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を含めるにはどうすればよいですか)。オペラや古い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));
}
});