我希望尝试计算2个datepicker字段之间的天数,并在输入字段中显示该值。
如何获得两个日期之间的天数datepicker和手动选择?
本工作手册仅选择:
$(function() {
$("#arrdatepicker").datepicker({
showOn: "button",
buttonImage: "calendar-ico.gif",
buttonImageOnly: true,
onSelect: function(dateText, inst) {
//dateText comes in as MM/DD/YY
var datePieces = dateText.split('/');
var month = datePieces[0];
var day = datePieces[1];
var year = datePieces[2];
//define select option values for
//corresponding element
$('select#arrmonth').val(month);
$('select#arrday').val(day);
$('select#arryear').val(year);
}
});
$("#depdatepicker").datepicker({
showOn: "button",
buttonImage: "calendar-ico.gif",
buttonImageOnly: true,
onSelect: function(dateText, inst) {
//dateText comes in as MM/DD/YY
var datePieces = dateText.split('/');
var month = datePieces[0];
var day = datePieces[1];
var year = datePieces[2];
//define select option values for
//corresponding element
$('select#depmonth').val(month);
$('select#depday').val(day);
$('select#depyear').val(year);
}
});
});
function dateDifference() {
var ev,honap,nap;
var datDate1 = new Date();
var datDate2 = new Date();
var napok;
ev = document.booking.arrivalyear.value;
honap = document.booking.arrivalmonth.value-1;
nap = document.booking.arrivalday.value;
datDate1.setFullYear(ev,honap,nap);
ev = document.booking.departureyear.value;
honap = document.booking.departuremonth.value-1;
nap = document.booking.departureday.value;
datDate2.setFullYear(ev,honap,nap);
napok = Math.floor((datDate2-datDate1)/(24*60*60*1000));
document.getElementById('label').innerHTML=napok+" nights";
}
答案 0 :(得分:2)
function dateDifference() {
// check if both is not empty
if($("#depdatepicker").val()=='' || $("#depdatepicker").val()=='') return;
var diff = ($("#depdatepicker").datepicker("getDate") -
$("#arrdatepicker").datepicker("getDate")) /
1000 / 60 / 60 / 24; // days
$('#label').html(diff+" nights");
}
获取两个日期,从到达减去dep,得到毫秒差异。
除以1000毫秒,60秒,60分钟,24小时 - 你得到的天数。