0
$(function () {
    $("#from").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onClose: function (selectedDate) {
            $("#to").datepicker("option", "minDate", selectedDate);
        }
    });
    $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onClose: function (selectedDate) {
            $("#from").datepicker("option", "maxDate", selectedDate);
        }
    });
});

$("#to").change(function () {
    var final = $("#to").datepicker("getDate");
    var start = $("#from").datepicker("getDate");
    var days = new Date(final - start);
    alert(days);
});

<label for="from">From</label>
<input type="text" id="from" name="from" />
<label for="to">to</label>
<input type="text" id="to" name="to" />

This is what I have by far, I pretend to get the number of days passed between 2 dates everytime that final date is filled. I'm having some trouble because of conversions :s

Fiddle

4

2 回答 2

4

我相信最大的事情是它datepicker('getDate')已经返回了一个 Date 对象,因此final并且start已经是 date 对象。

现在您有两个日期对象,您可以在以下线程中编写代码示例来获取它们之间的天数

如何使用 JavaScript 计算两个日期之间的天数?

以下代码直接从线程和google中翻录:您可以适当填写

var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2008,01,12);
var secondDate = new Date(2008,01,22);

var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));
于 2013-04-11T14:03:20.173 回答
0

以下示例对我来说效果很好。

$(document).ready(function(){
   var $datepicker1 =  $( "#firstday" );
   var $datepicker2 =  $( "#lastday" );
   $datepicker1.datepicker();
   $datepicker2.datepicker({
       onClose: function() {
           var fromDate = $datepicker1.datepicker('getDate');
           var toDate = $datepicker2.datepicker('getDate');
           // date difference in millisec
           var diff = new Date(toDate - fromDate);
           // date difference in days
           var days = diff/1000/60/60/24;

           alert(days);
       }
   });
});

参考:-计算 jquery datepicker 中两个日期之间的天数

于 2014-12-03T17:33:07.043 回答