twitter-bootstrap minDate 在引导日期选择器中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19219713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 18:46:27  来源:igfitidea点击:

minDate not working in bootstrap datepicker

twitter-bootstrapdatepicker

提问by pynovice

I am using this bootstrap datepicker http://www.eyecon.ro/bootstrap-datepicker/:

我正在使用这个 bootstrap datepicker http://www.eyecon.ro/bootstrap-datepicker/

<script type="text/javascript">
var dateToday = new Date();
$(function() {
    $("#id_deadline").datepicker({minDate: dateToday});
});
</script>

I want to let select the date from today to future only (not a past date). Datepicker is showing up and it is letting me select the past date too. What's wrong?

我只想选择从今天到未来的日期(不是过去的日期)。Datepicker 出现了,它也让我选择过去的日期。怎么了?

回答by tetri

Try using that way:

尝试使用这种方式:

$("#id_deadline").datepicker({ startDate: "today" });

There is no need a Date var for using startDate parameter.

使用 startDate 参数不需要 Date 变量。

回答by Bikal Basnet

Since none of the answers worked for me. I managed to disable to older dates by using onRenderoption

因为没有一个答案对我有用。我设法通过使用onRender选项禁用旧日期

$('.datepicker').datepicker({
          onRender: function(date) {
            return date.valueOf() < new Date().valueOf() ? 'disabled' : '';
        }
    }); 

回答by Mohamed

try using startDatewith this way :

尝试使用startDate这种方式:

$("#id_deadline").datepicker({startDate: new Date()});

I hope it will help you

我希望它会帮助你

回答by shashik493

My code is Bootstrap Datetimepicker (mindate) Functionality related. My code is working in all browser like ff,chrome and ie also. Please check below example. Working for me.

我的代码与 Bootstrap Datetimepicker (mindate) 功能相关。我的代码适用于所有浏览器,如 ff、chrome 和 ie。请检查以下示例。对我来说有效。

    $('#datetimepicker1').datetimepicker({
            //minDate: new Date() + 1,
            minDate:moment(),
            Default: true,
            defaultDate: new Date(),
            pickTime: false,
            use24hours: false,
            format: 'MM-DD',
            icons: {
                time: "fa fa-clock-o",
                date: "fa fa-calendar",
                up: "fa fa-arrow-up",
                down: "fa fa-arrow-down"
            },
            //   minDate: moment(+1),
        });

回答by Waruna Manjula

try,

尝试,

var dateToday = new Date(); 
$(function () {
    $( "#datepicker1" ).datepicker({
        format: 'yyyy-mm-dd',
      autoclose: true,
      startDate: dateToday
    });
});

Backdate

回溯

var last7Days = new Date(); 
last7Days.setDate(last7Days.getDate()-7);
$(function () {
    $( "#datepicker1" ).datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        startDate: last7Days
    });
});

回答by Ashwin

Following should work: minDate: new Date()

以下应该有效: minDate: new Date()

回答by Sl4rtib4rtf4st

I to spent some time looking through the documentation in search for this option. The bootstrap datepicker comes with a startDateoption that will accept dates or strings, from the documentation:

我花了一些时间浏览文档以搜索此选项。bootstrap datepicker 带有一个startDate选项,该选项将接受文档中的日期或字符串:

Date should be in local timezone. String must be parsable with format

日期应该在当地时区。字符串必须是可解析的format

There is an online demothat lets you try out different options. If you enter now(or new Date()) in the startDate field, the datepicker will not let you select a date before the current date.

有一个在线演示,可让您尝试不同的选项。如果您在 startDate 字段中输入now(或new Date()),日期选择器将不会让您选择当前日期之前的日期。

The little code output at the side will then show you the code you need to use in you website, in this case that would be:

旁边的小代码输出将显示您需要在您的网站中使用的代码,在这种情况下将是:

$('#sandbox-container input').datepicker({
    startDate: "now"
});

You do need to switch the #sandbox-container inputpart for a selector that points to your datepicker element.

您确实需要切换#sandbox-container input指向日期选择器元素的选择器的部分。

回答by holymoof

In case you want to limit datepicker min/max date after initialization (for example when some event fires), you can use: setStartDateor setEndDatelike following:

如果您想在初始化后限制 datepicker 最小/最大日期(例如,当某些事件触发时),您可以使用: setStartDatesetEndDate,如下所示:

$('#datepicker').datepicker('setStartDate', new Date());
$('#datepicker').datepicker('setEndDate', new Date());

See method usage docs: https://bootstrap-datepicker.readthedocs.io/en/latest/methods.html#methods

查看方法使用文档:https: //bootstrap-datepicker.readthedocs.io/en/latest/methods.html#methods

回答by Manoj Salvi

you can use following working code which disables all the past dates-

您可以使用以下禁用所有过去日期的工作代码-

for DatePicker working Fiddle

用于 DatePicker工作小提琴

$(function () {
  //get Today's Date
  var currDate = new Date();
  $('#datetimepicker').datepicker({
    startDate : currDate
  });
});

and for DateTimePicker this would work (Fiddle)-

对于 DateTimePicker,这将起作用(小提琴)-

$(function () {
  $('#datetimepicker').datetimepicker({
    minDate : moment()
  });
});