为什么日期差从零开始

时间:2018-08-14 06:58:17

标签: javascript jquery

实施开始日期和结束日期以查找日期差。如果我选择今天的开始日期和今天的结束日期,则差异显示为0,并且我也禁用了周末,尽管它也计算了周末。这是我的代码。

$("#startDate").datepicker({

  beforeShowDay: $.datepicker.noWeekends,
  changeMonth: true,
  todayHighlight: true,

  minDate: 0,
  numberOfMonths: 1,
  todayHighlight: true,
  
  onSelect: function(dateStr) {
    var min = $(this).datepicker('getDate'); // Get selected date
    $("#endDate").datepicker('option', 'minDate', min || '0'); // Set other min, default to today
  }
});

$("#endDate").datepicker({

  beforeShowDay: $.datepicker.noWeekends,
  changeMonth: true,
  endDate: true,
  todayHighlight: true,
  minDate: 0,
  numberOfMonths: 1,
  buttonImageOnly: true,
  onSelect: function(dateStr) {

    var max = $(this).datepicker('getDate'); // Get selected date
    $('#datepicker').datepicker('option', 'maxDate');
    var start = $("#startDate").datepicker("getDate");
    var end = $("#endDate").datepicker("getDate");
    var days = (end - start) / (1000 * 60 * 60 * 24);
    $("#noofDays").val(days);

  }
});

1 个答案:

答案 0 :(得分:0)

在jquery中使用了日期差计算功能。它对我有用

$("#startDate").datepicker({
  beforeShowDay: $.datepicker.noWeekends,
  minDate: 0,
  numberOfMonths: 1,
  maxDate: '+1Y+6M',
  onSelect: function(dateStr) {
    var min = $(this).datepicker('getDate'); // Get selected date
    $("#endDate").datepicker('option', 'minDate', min || '0'); // Set other min, default to today
  }
});

$("#endDate").datepicker({
  beforeShowDay: $.datepicker.noWeekends,
  minDate: 0,
  numberOfMonths: 1,
  onSelect: function(dateStr) {
    var max = $(this).datepicker('getDate'); // Get selected date
    $('#datepicker').datepicker('option', 'maxDate', max || '+1Y+6M'); // Set other max, default to +18 months
    var start = $("#startDate").datepicker("getDate");
    var end = $("#endDate").datepicker("getDate");
    var days = (end - start) / (1000 * 60 * 60 * 24);
    $("#noofDays").val(days);
  }
});