简体   繁体   English

在 Jquery Ui Datepicker 中禁用今天之后的未来日期

[英]Disable future dates after today in Jquery Ui Datepicker

I want to disable all the future dates after today in Jquery Ui Datepicker我想在 Jquery Ui Datepicker 中禁用今天之后的所有未来日期

Here is the Demo :这是演示

Code :代码 :

$( "#start_date" ).datepicker(

        { 
            maxDate: '0', 
            beforeShow : function()
            {
                jQuery( this ).datepicker('option','maxDate', jQuery('#end_date').val() );
            },
            altFormat: "dd/mm/yy", 
            dateFormat: 'dd/mm/yy'

        }

);

$( "#end_date" ).datepicker( 

        {
            maxDate: '0', 
            beforeShow : function()
            {
                jQuery( this ).datepicker('option','minDate', jQuery('#start_date').val() );
            } , 
            altFormat: "dd/mm/yy", 
            dateFormat: 'dd/mm/yy'

        }

);

Try this尝试这个

 $(function() {
  $( "#datepicker" ).datepicker({  maxDate: new Date() });
 });

Or you can achieve this using as below:或者您可以使用以下方法实现此目的:

$(function() {
  $( "#datepicker" ).datepicker({  maxDate: 0 });
});

Reference参考

DEMO演示

UPDATED ANSWER更新的答案

This worked for me endDate: "today"这对我endDate: "today"

  $('#datepicker').datepicker({
        format: "dd/mm/yyyy",
        autoclose: true,
        orientation: "top",
        endDate: "today"

  });

SOURCE来源

就我而言,我已将此属性赋予输入标签

data-date-start-date="0d" data-date-end-date="0d"

You can simply do this你可以简单地做到这一点

$(function() {
    $( "#datepicker" ).datepicker({  maxDate: new Date });
  });

JSFiddle JSFiddle

FYI: while checking the documentation , found that it also accepts numeric values too.仅供参考:在检查文档时,发现它也接受数值。

Number : A number of days from today.数量:从今天起的天数。 For example 2 represents two days from today and -1 represents yesterday.例如,2 代表从今天起两天,-1 代表昨天。

so 0 represents today.所以0代表今天。 Therefore you can do this too因此你也可以这样做

 $( "#datepicker" ).datepicker({  maxDate: 0 });

Change maxDate to current date将 maxDate 更改为当前日期

maxDate: new Date()

It will set current date as maximum value.它将当前日期设置为最大值。

In case you are appending Dtpicker,use the following code如果您要附加 Dtpicker,请使用以下代码

$('#enddate').appendDtpicker({
    "dateOnly": true,
    "dateFormat": "YYYY-MM-DD",
    "closeOnSelected": true,
    maxDate: new Date()         
});

//Disable future dates after current date //禁用当前日期之后的未来日期

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

//Disable past dates after current date //禁用当前日期之后的过去日期

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

Datepicker does not have a maxDate as an option. maxDate没有maxDate作为选项。 I used this endDate option.我使用了这个 endDate 选项。 It worked well.它运作良好。

$('.demo-calendar-default').datepicker({
  autoHide: true,
  zIndex: 2048,
  format: 'dd/mm/yyyy',
  endDate: new Date()
});
maxDate: new Date() 

它对我来说工作正常,在日期范围选择器中禁用当前日期

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

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