简体   繁体   English

Bootstrap 3 日期选择器 - minDate 和 maxDate

[英]Bootstrap 3 datepicker - minDate and maxDate

http://eonasdan.github.io/bootstrap-datetimepicker/ http://eonasdan.github.io/bootstrap-datetimepicker/

Hi你好

I am trying to use the min max date options but not sure how to use it as the documentation doesn't provide example code.我正在尝试使用 min max date 选项,但不确定如何使用它,因为文档没有提供示例代码。

I found on stackoverflow that you can do this:我在 stackoverflow 上发现你可以这样做:

minDate: moment()分钟日期:时刻()

But that throws an error that moment is not defined.但这会引发未定义时刻的错误。

I am guessing it can't find the moment.js which is included on the page otherwise the plugin wouldn't work.我猜它找不到页面中包含的 moment.js,否则插件将无法工作。

What I am trying to achieve is to disable 10 days before the current day and show 30 days from the current day.我想要实现的是在当天前 10 天禁用并显示从当天起 30 天。

Thanks谢谢

 Here is the code (I have removed everything except whats important to simply show the code): window[ns] = window[ns] || {}; (function ($, moment, app) { 'use strict'; // Private Methods var nameSpace = 'global', globalDataObject = null, notifyRenderCallbacks = function (pageName) { if ($('.js-calendar').length) { $('.js-calendar').datetimepicker({ format: 'DD MMMM YYYY', dayViewHeaderFormat: 'MMMM YYYY', icons: { next: 'icon icon-chevronright', previous: 'icon icon-chevronleft' }, enabledDates: moment().add(30, 'days') }); } }, // If this module requires global data app.register(nameSpace, initialize); }(jQuery, moment, window[ns] || {}));

To answer my question.回答我的问题。 The framework used required you to add moment to a config file otherwise it would not allow to use http://momentjs.com/ or any other JS - spoke to the person who set up the framework and they did it for security reasons.使用的框架要求您向配置文件添加 moment,否则将不允许使用http://momentjs.com/或任何其他 JS - 与设置框架的人交谈,他们出于安全原因这样做了。

Kasun's answer is correct but a better way to do this is to use Moment.js since datetimepicker is using this JS. Kasun 的回答是正确的,但更好的方法是使用 Moment.js,因为 datetimepicker 正在使用这个 JS。

So to answer the 2nd part which is to disable 10 days before the current day and show 30 days from the current day you need to set the options as below.因此,要回答第二部分,即禁用当前日期前 10 天并显示当前日期后 30 天,您需要设置如下选项。 I didn't need to do the 10 days before but wanted only a set number of days to be available to select.我不需要在 10 天之前做,但只希望可以选择一组天数。 I've added comments to make things clear but they shouldn't be there.我已经添加了注释来使事情清楚,但它们不应该在那里。

$mydatepicker.datetimepicker({
    minDate: moment(), // Current day
    maxDate: moment().add(30, 'days'), // 30 days from the current day
    viewMode: 'days',
    format: 'DD MMMM YYYY',
    dayViewHeaderFormat: 'MMMM YYYY',
});

What the above will do is enable all the days between minDate and maxDate.上面要做的是启用 minDate 和 maxDate 之间的所有日子。

You could also directly enter the date range:您也可以直接输入日期范围:

$mydatepicker.datetimepicker({
    minDate: moment("12/01/2015"),
    maxDate: moment("12/30/2015"),
    viewMode: 'days',
    format: 'DD MMMM YYYY',
    dayViewHeaderFormat: 'MMMM YYYY',
});

And this will disable all days before and after the dates you entered into moment().这将禁用您输入 moment() 的日期之前和之后的所有天数。

This is not part of the question but I wanted to display the current day in the input value when the calendar loaded but it wouldn't, instead it would show the placeholder text.这不是问题的一部分,但我想在日历加载时在输入值中显示当前日期,但它不会,而是显示占位符文本。 I think its because of the use if minDate so I simply used jQuery to replace the value of the input.我认为这是因为使用 if minDate 所以我只是使用 jQuery 来替换输入的值。

$datepickerInput.val(moment());
var todayDate = new Date().getDate();

$('#element').datetimepicker({
  timepicker:false,
  formatDate:'Y/m/d',
  minDate: new Date(),
  maxDate: new Date(new Date().setDate(todayDate + 30))
});

By using this example you can give option to user to select date between today and next 30 days without using momentjs.通过使用此示例,您可以让用户选择今天和接下来 30 天之间的日期,而无需使用 momentjs。 You can provide value of minDate and maxDate accoording to you.您可以根据自己的需要提供 minDate 和 maxDate 的值。 For an example if you want to give options in between last 30 days to next 30 days, so set例如,如果您想在过去 30 天到未来 30 天之间提供选项,请设置

minDate: new Date(new Date().setDate(todayDate - 30))
maxDate: new Date(new Date().setDate(todayDate + 30))

就这样,使用 JavaScript 日期对象:

minDate: (new Date()).getDate()

For disable对于禁用

$("#editreservation_to").datetimepicker({minDate:-1,maxDate:-2}).attr('readonly','readonly');  

For Enable启用

$("#editreservation_to").datetimepicker({minDate:false,maxDate:false}).removeAttr('readonly');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM