3

再会,

从 jQuery datepicker 选择日期后,我在计算从现在到选定日期的天数时遇到了一些麻烦。

我认为日期选择器正在更改选择日期的日期格式并导致计算出错,但我不知道如何解决这个问题。我确定是我,但我看不到森林只见树木,所以任何建议都会非常有帮助。

这是代码:

<input type="text" class="datepicker-calc" />
<input type="text" id="tbAddDays" />
$(document).ready(function () {
    $(".datepicker-calc").datepicker({
        dateFormat: "dd/mm/yy",
        changeMonth: true,
        changeYear: true,
        onSelect: function (date, e) {
            var expDate = new Date(date);
            var date = e.selectedDay;
            if (date < 10) {
                date = ("0" + e.selectedDay)
            }
            var month = e.selectedMonth;
            if (month < 10) {
                month = ("0" + e.selectedMonth)
            }
            var year = e.selectedYear;
            var endDate = month + "/" + date + "/" + year;
            updateDays(endDate, e);
            $(".datepicker-calc").datepicker("hide");
        }
    });

    function treatAsUTC(date) {
        var result = new Date(date);
        result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
        return result;
    }

    function updateDays(expDate, e) {
        var now = new Date();
        var startDate = now.toDateString('dd/MM/yyyy');
        var exp = new Date(expDate);
        var endDate = exp.toDateString('dd/MM/yyyy');
        var millisecondsPerDay = 24 * 60 * 60 * 1000;
        var totalDays = (treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay;
        $('#tbAddDays').val(totalDays);
    }
});
4

2 回答 2

2

onSelect事件将接收指定格式的字符串,即dd/mm/yy. JavaScriptDate.parse方法会错误地解析这些日期,或者根本不解析它们。解决方案是使用parseDate()jQuery ui 中内置的函数:

onSelect: function (dateText, inst) {
  var expDate = $.datepicker.parseDate(inst.settings.dateFormat, dateText, inst.settings);
  var diff = expDate - new Date();
  var days = diff / 1000 / 60 / 60 / 24;
  alert( /* ceil, floor, round */ days);
}
于 2013-01-10T22:04:52.007 回答
0

意识到我需要显示为 jQuery dd/mm/yy (dd/mm/yyyy) 格式,所以保留它并对传递给 updateDays() 方法的 expDate 进行了一些操作,以确保要计算的日期会由JS正确处理。

工作脚本:

$(".datepicker-calc").datepicker({
      dateFormat: "dd/mm/yy",
      changeMonth: true,
      changeYear: true,
      onSelect: function (selectdate, e) {
      var newDate = selectdate.slice(0,2);
      var newMonth = selectdate.slice(4,6);
      if (newMonth < "10") { newMonth = ("0" + newMonth.slice(0,1));};
      var newYear = selectdate.slice(6,11);
      var expDate = newMonth + "/" + newDate + "/" + newYear;
      updateDays(expDate, e);
      $(".datepicker-calc").datepicker("hide");
   }
});

function treatAsUTC(date) {
    var result = new Date(date);
    result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
    return result;
}

function updateDays(expDate, e) {
    var now = new Date();
    var startDate = now.toDateString('dd/MM/yyyy');
    var expire = new Date(expDate);
    var endDate = expire.toDateString('dd/MM/yyyy');
    var millisecondsPerDay = 24 * 60 * 60 * 1000;
    var totalDays = (treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay;
    $('#tbAddDays').val(totalDays);
}
于 2013-01-11T10:57:33.167 回答