1

我正在尝试计算 2 个日期选择器字段之间的天数,并在输入字段中显示该值。

如何获取两个日期日期选择器和手动选择之间的天数?

本工作手册仅选择:

$(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";
}
4

1 回答 1

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 小时 - 你得到天数。

http://jsfiddle.net/samliew/5SCM5/9/

于 2013-04-21T07:20:45.890 回答