简体   繁体   English

jQuery ui datepicker-如何在一个datepicker中设置两个最小/最大日期限制?

[英]jquery ui datepicker - how to set two min/max date restrictions in one datepicker?

I am using the jquery ui datepicker with select date range. 我正在使用jquery ui datepicker和选择日期范围。 I know that by default it already set if the from picks a date then the to date can not pick any date before the from date picked. 我知道默认情况下已经设置好了,如果from选择日期,那么to date不能选择起始日期之前的任何日期。 I also checked the minDate and maxDate documents but I still couldn't try figuring it out. 我还检查了minDatemaxDate文档,但仍然无法尝试解决。

I want to keep the default setting it has which is after date from is chosen date to cannot be before the from date vise versa but also want to add another restriction which is both datepickers have a maxDate of 0 which is today. 我想保留它的默认设置,该默认设置是从选择的日期算起的日期,不能在从日期算起的日期之前,也要添加另一个限制,这两个日期选择器的maxDate都是今天的0。 None of them can be picked pass today. 今天没有人可以通过。

This is pretty much just the standard. 这几乎只是标准。

  $( "#date-from-field" ).datepicker({
    onClose: function( selectedDate ) {
      $( "#date-to-field" ).datepicker( "option", "minDate", selectedDate );
    }
  });
  $( "#date-to-field" ).datepicker({
    onClose: function( selectedDate ) {
      $( "#date-from-field" ).datepicker( "option", "maxDate", selectedDate );
    }
  });

I tried adding these two but none of them works though 我尝试添加这两个,但它们都不起作用

$( "#date-from-field" ).datepicker({maxDate: "0"});
$( "#date-from-field" ).datepicker({maxDate: "+0m +0w"});

but none of them work though. 但它们都不起作用。

Thank you in advance. 先感谢您。

Alright so pretty much you need to check if the selectedDate is empty when date-to-field is updated and make the maxDate to "0". 好了,您几乎需要检查在更新日期至字段时selectedDate是否为空并将maxDate设置为“ 0”。 Once you do it should act as you wanted, it'll set the max to today's date or if the date of the from if it's not todays date. 一旦完成,它就会按照您想要的方式运行,它将最大值设置为今天的日期,或者将起始日期设置为不是今天的日期。 Here's a codepen that works for me - CodePen . 这是一个适用于我的codepen - CodePen

    $("#date-from-field").datepicker({
  onClose: function( selectedDate ) {
      $( "#date-to-field" ).datepicker( "option", "minDate", selectedDate );
    },
  maxDate: "0"
});

$("#date-to-field").datepicker({
  onClose: function( selectedDate ) {
    $( "#date-from-field" ).datepicker( "option", "maxDate", selectedDate ? selectedDate: "0" );
    },
  maxDate: "0"
});

EDIT 编辑

Updated the CodePen a bit more so that it checks if the selected date is greater than todays date. 更新了CodePen,以便检查所选日期是否大于今天的日期。

$("#date-to-field").datepicker({
  onClose: function( selectedDate ) {
    var possibleDate = new Date(selectedDate);
    possibleDate = (possibleDate < new Date())?possibleDate: new Date();
    $( "#date-from-field" ).datepicker( "option", "maxDate", selectedDate ? possibleDate: "0" );
    },
  maxDate: "0"
});

You can refer to this link : http://api.jqueryui.com/datepicker/#option-maxDate 您可以参考以下链接: http : //api.jqueryui.com/datepicker/#option-maxDate

This is to Initialize the datepicker with the maxDate option specified, not to set it up afterwards: 这是为了使用指定的maxDate选项初始化日期选择器,而不是之后进行设置:

$( ".selector" ).datepicker({
  maxDate: "+1m +1w"
});

To modify/get the option, use this: 要修改/获取选项,请使用以下命令:

Get or set the maxDate option, after initialization: 初始化后获取或设置maxDate选项:

// Getter
var maxDate = $( ".selector" ).datepicker( "option", "maxDate" );

// Setter
$( ".selector" ).datepicker( "option", "maxDate", "+1m +1w" );

"+0" is for now() 目前为"+0" now()

Same for mindate! 同样要注意!

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

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