Eonasdan Bootstrap Datetimepicker - 无法使用当前时间将 minDate 设置为现在

Eonasdan Bootstrap Datetimepicker - Can't set minDate to now with current time

我需要将最小日期设置为当前时间。但它只有在我设置没有时间的日期时才有效。我尝试了几种时间格式,但没有找到解决方案。

这是我的设置

默认:

settings: {
    locale: 'de',
    viewMode: 'days',
    format: 'DD. MMMM YYYY',
    useStrict: true,
    focusOnShow: false,
    showClose: true,
},

日期选择器设置:

this.$time_from.datetimepicker($.extend(true, DefaultDatepicker.settings, {
    minDate: DefaultDatepicker.getDateRange(0),
    maxDate: DefaultDatepicker.getDateRange(365),
    format: 'DD. MMMM YYYY - HH:mm',
    viewMode: 'days'
}));

输出:08。 2016 年 11 月 - 00:00

计算日期的函数:

getDateRange: function (max) {
    var CurDate = new Date();
    CurDate.setDate(CurDate.getDate() + max);
    var mm = CurDate.getMinutes();
    var hh = CurDate.getHours();
    var dd = CurDate.getDate();
    var mm = CurDate.getMonth() + 1;
    var y = CurDate.getFullYear();

    //return mm + '/' + dd + '/' + y + ' ' + hh + ':' + mm + ':' + ss; //not working output: 2016-11-08T14:18:45+01:00 + error

    return mm + '/' + dd + '/' + y;
},
//Example getDateRange(0) = now, getDateRange(1) = tomorrow

已测试: 也不工作

newgetDateRange: function (max) {
    var date = new Date();
    date.setDate(date.getDate() + max);
    var ss = ( '0' + (date.getSeconds())).slice(-2);
    var mm = ( '0' + (date.getMinutes())).slice(-2);
    var hh = ( '0' + (date.getHours())).slice(-2);
    var d = ( '0' + (date.getDate())).slice(-2);
    var M = ( '0' + (date.getMonth() + 1) ).slice(-2);
    var Y = date.getFullYear();

    var $new = Y + '-' + M + '-' + d + 'T' + hh + ':' + mm + ':' + ss + 'Z';
    return $new;
},

我认为你的问题是 getDateRange 函数结果的格式,可能应该 return DD. MMMM YYYY - HH:mm 格式的日期。

文档 says (for the date function):

Parsing of the newDate parameter is made using moment library with the options.format and options.useStrict components configuration.

我认为它对 minDate and maxDate 选项使用相同的逻辑。

无论如何 minDatemaxDate 都接受 moment 对象,所以你的 getDateRange 函数可以 return moment 而不是字符串,你可以简单地使用 add() 得到你的范围。

这是一个工作示例:

var DefaultDatepicker = {
  settings: {
    locale: 'de',
    viewMode: 'days',
    format: 'DD. MMMM YYYY',
    useStrict: true,
    focusOnShow: false,
    showClose: true,
  },
  getDateRange: function(max){
    return moment().add(max, 'days');
  }
};

$('#datetimepicker').datetimepicker($.extend(true, DefaultDatepicker.settings, {
    minDate: DefaultDatepicker.getDateRange(0),
    maxDate: DefaultDatepicker.getDateRange(365),
    format: 'DD. MMMM YYYY - HH:mm',
    viewMode: 'days'
}));
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.42/css/bootstrap-datetimepicker.css" rel="stylesheet"/>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/locale/de.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.42/js/bootstrap-datetimepicker.min.js"></script>

<div class="input-group date" id="datetimepicker">
  <input type="text" class="form-control" />
  <span class="input-group-addon">
    <span class="glyphicon glyphicon-calendar"></span>
  </span>
</div>