【问题标题】:jQuery Date Picker - changing the date relative to each otherjQuery Date Picker - 更改相对于彼此的日期
【发布时间】:2016-10-23 21:43:50
【问题描述】:

您好,我正在尝试创建一个预订表单,用户只能在其中选择 1 到 14 天(从当前日期 +1)。我遇到的问题是 EndDate(此处为#til)设置为从今天开始的 14 天。因此,如果我在 StartDate(此处为 #fra)继续一个月,我无法在 EndDate 中选择任何内容。

如何使 EndDate 相对于 StartDate 设置?

$( function valgavdato() {
var dateFormat = "mm/dd/yy",

    from = $( "#fra" )
      .datepicker({
        minDate: '0+',
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1
      })
      .on( "change", function() {
        to.datepicker( "option", "minDate", getDate( this ) );
      }),
    to = $( "#til" ).datepicker({
      maxDate: '14+',
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 1
    })
    .on( "change", function() {
      from.datepicker( "option", "maxDate", getDate( this ) );
    });

  function getDate( element ) {
    var date;
    try {
      date = $.datepicker.parseDate( dateFormat, element.value );
    } catch( error ) {
      date = null;
    }

    return date;
  }
} );

【问题讨论】:

  • 使用选择事件更新其他日期选择器

标签: jquery jquery-ui datepicker jquery-ui-datepicker


【解决方案1】:

问题是maxDate 在加载时设置为“今天”+14。
您必须计算 14 天 + 选择的日期...

所以,这里有一些工作。
可能不是最干净的编码方式...
但仍然......它有效。

唯一的更改(添加)在 #fra on change。

$( function valgavdato() {
    var dateFormat = "mm/dd/yy",
        to_MaxDate = 14;    // From date + this = to maxDate

        from = $( "#fra" )
    .datepicker({
        minDate: '0+',
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1
    })
    .on( "change", function() {
        var PickedDate = getDate( this );
        // See that date is in UTC format.
        console.log( "From DatePicker: "+JSON.stringify(PickedDate) );
        
        // Process the picked date.
        var tempDay = PickedDate.getDate() + to_MaxDate; // Add a number of days to the picked date.
        var tempMonth = PickedDate.getMonth() + 1;   // Because months are zero based.
        var tempYear = PickedDate.getYear() + 1900; // Because years are 1900 based
        console.log( "Temp date: "+ tempYear+"/"+tempMonth+"/"+tempDay +" --- It may look impossible... But Date() handles it.");

        // Create a date object in a UTC format.
        var newMaxDate = new Date(Date.UTC(tempYear, tempMonth-1, tempDay, 23, 59, 59));
        console.log(  "New max date: : "+ JSON.stringify(newMaxDate) );

        // Set the minDate ans maxDate options.  
        to.datepicker( "option", {"minDate": PickedDate, "maxDate": newMaxDate});

    }),
        to = $( "#til" ).datepicker({
            maxDate: '14+',
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 1
        })
    .on( "change", function() {
        from.datepicker( "option", "maxDate", getDate( this ) );
    });

    function getDate( element ) {
        var date;
        try {
            date = $.datepicker.parseDate( dateFormat, element.value );
        } catch( error ) {
            date = null;
        }

        return date;
    }
} );
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>



<input type="text" id="fra"><br>
<br>
<input type="text" id="til">

【讨论】:

  • 感谢您的回答。但你的回答给了我 16 天而不是 14 天。
  • 你确定你数对了吗?我看到的是所选日期(未计算在内)+ 14 天(就像默认情况下没有考虑所选日期一样)。如果您只想要总共 14 个可选日期,只需将 var newMaxDate = new Date(Date.UTC(tempYear, tempMonth-1, tempDay, 23, 59, 59)); 更改为 var newMaxDate = new Date(Date.UTC(tempYear, tempMonth-1, tempDay, 0, 0, 0));
  • 非常感谢您的回答,现在可以完美运行了 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
  • 2013-12-12
  • 2011-11-03
相关资源
最近更新 更多