jQuery Bootstrap日期选择器禁用没有当前日期的过去日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16123056/
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-08-26 16:29:26  来源:igfitidea点击:

Bootstrap datepicker disabling past dates without current date

javascriptjquerytwitter-bootstrap

提问by itskawsar

I wanted to disable all past date before current date, not with current date. I am trying by bootstrap datepicker library "bootstrap-datepicker" and using following code:

我想禁用当前日期之前的所有过去日期,而不是当前日期。我正在尝试通过 bootstrap datepicker 库“ bootstrap-datepicker”并使用以下代码:

$('#date').datepicker({ 
    startDate: new Date() 
});

It works fine. But it is disabling date till today.

它工作正常。但它是禁用日期直到今天。

As example if today is 04-20-2013 and i disable past dates by setting startDate: new Date(). but I am able to select date from 04-21-2013.

例如,如果今天是 04-20-2013,我通过设置 startDate: new Date() 禁用过去的日期。但我可以选择 04-21-2013 的日期。

UPDATED: i can solve it as following for UTC zone:

更新:对于UTC区域,我可以按以下方式解决它:

var d = new Date();
options["startDate"] = new Date(d.setDate(d.getDate() - 1));

or startDate: "+0d"

或者 startDate: "+0d"

But these methods don't work when UTC is a day ahead. For my client in California that means at 5:00 pm my client can no longer select his local current date as a valid date. In order to fix this I am temporarily using startDate: "-1d", but of course before 5 that means yesterday is visible.

但是当 UTC 提前一天时,这些方法不起作用。对于我在加利福尼亚的客户,这意味着在下午 5:00 我的客户不能再选择他当地的当前日期作为有效日期。为了解决这个问题,我暂时使用了startDate: "-1d",但当然在 5 之前这意味着昨天是可见的。

Has anyone come up with a better method for now as I do not want to tell users to put in a UTC date?

现在有没有人想出更好的方法,因为我不想告诉用户输入 UTC 日期?

Thanks in advance.

提前致谢。

回答by Tom Hartman

var date = new Date();
date.setDate(date.getDate()-1);

$('#date').datepicker({ 
    startDate: date
});

回答by Jignesh Gohel

HTML

HTML

<input type="text" name="my_date" value="4/26/2015" class="datepicker">

JS

JS

jQuery(function() {
  var datepicker = $('input.datepicker');

  if (datepicker.length > 0) {
    datepicker.datepicker({
      format: "mm/dd/yyyy",
      startDate: new Date()
    });
  }
});

That will highlight the default date as 4/26/2015 (Apr 26, 2015) in the calendar when opened and disable all the dates before current date.

这将在日历中突出显示默认日期为 4/26/2015(2015 年 4 月 26 日),并禁用当前日期之前的所有日期。

回答by Sebastian

Use minDate

使用minDate

var date = new Date();
var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());

$('#date').datepicker({ 
    minDate: today
});

回答by Ryan Knol

use

startDate: '-0d'

Like

喜欢

$("#datepicker").datepicker({
    startDate: '-0d',
    changeMonth: true
});

回答by dcalvert

You can use the data attribute:

您可以使用数据属性:

<div class="datepicker" data-date-start-date="+1d"></div>

回答by cfprabhu

var nowDate = new Date();
var today = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 0, 0, 0, 0);
 $('#date').datetimepicker({
  startDate: today
 });

回答by Chaitanya K

<script type="text/javascript">
$('.datepicker').datepicker({
    format: 'dd/mm/yyyy',
    todayHighlight:'TRUE',
    startDate: '-0d',
    autoclose: true,
})

回答by Developer

Here it is

这里是

 <script>
          $(function () {
              var date = new Date();
              date.setDate(date.getDate() - 7);

            $('#datetimepicker1').datetimepicker({
                maxDate: 'now',
                showTodayButton: true,
                showClear: true,
                minDate: date
            });
        });
    </script>

回答by Lakhan

Try it :

尝试一下 :

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

回答by akpp

The solution is much simpler:

解决方案要简单得多:

$('#date').datepicker({ 
    startDate: "now()" 
});

Try Online Demoand fill input Start date: now()

尝试在线演示并填写输入开始日期:现在()