添加和删​​除当前时间的小时数,为datetimepicker创建无限循环

时间:2016-10-20 16:55:35

标签: javascript jquery datetimepicker bootstrap-datetimepicker eonasdan-datetimepicker

我正在使用eonasdan Bootstrap 3 datetimepicker,我在defaultDate选项中添加了4个小时。但是当我改变时,我想要将这4个小时的时间从它创建无限循环并显示错误的日期和时间。

代码正在关注

  var $datePickerOutput = $('#datePickerOutput'),
      d = moment();

  if ( $datePickerOutput.length ) {
    var defaults = {
      inline: true,
      sideBySide: true,
      useCurrent: false,
      defaultDate: d.add(3, 'hours'),
      minDate: d,
    };
    $datePickerOutput.datetimepicker(defaults);
    $datePickerOutput.on('dp.change', function(e){
      var $this = $(this);
      var presentDate  = new Date();
      var selectedDate = new Date(e.date);

      var pd = presentDate.getDate(),
          pm = presentDate.getMonth(),
          py = presentDate.getFullYear(),
          sd = selectedDate.getDate(),
          sm = selectedDate.getMonth(),
          sy = selectedDate.getFullYear();

      if ( ( pd+pm+py !== sd+sm+sy ) ) {
        console.log(moment(e.date).format('DD-MM-YYYY hh:mm A'));
        $(this).data("DateTimePicker").date(e.date.subtract(4, 'hours'));
      }

    });
  }

0 个答案:

没有答案