简体   繁体   English

根据日期选择器“开始日期”和输入的日期显示“结束日期”

[英]Display “End Date” based on datepicker “Start Date” and inputted date

Need answer as soon as possible! 需要尽快回答!

How to get a end date based on a inputted day interval and start date like if i have a start date that has a value of 4/19/2015 and the then the inputted day interval is like 10 how would I do that in jquery and the event will trigger if the value inside the datepicker input box changed. 如何基于输入的日期间隔和开始日期获取结束日期,例如,如果我的开始日期的值是4/19/2015 ,然后输入的日期间隔像10我将如何在jquery和如果datepicker输入框中的值发生更改,则事件将触发。

Guys I will appreciate all your help T_T. 伙计们,我将感谢您的所有帮助T_T。

you may refer to the answer How to add number of days to today's date? 您可以参考答案如何在今天的日期中增加天数?

Assuming, you are using jqueryui datepicker. 假设您正在使用jqueryui datepicker。 if so, use following code to get the selected date, as soon as the date is selected from the datepicker. 如果是这样,则从日期选择器中选择日期后,立即使用以下代码获取所选日期。

<script>
  $(function() {
    $( "#from" ).datepicker({
      onSelect: function( selectedDate ) {
        var startDate = selectedDate; // you got the startDate here
      }
    });
  });
 </script>

please find the solution below: 请在下面找到解决方案:

HTML: HTML:

<input type="text" id="start-date-picker" />
<input type="text" id="end-date-picker" />

JS: JS:

$('#start-date-picker').datepicker();
$('#end-date-picker').datepicker();


$('#start-date-picker').change(function(){
    var interval = 10;

  function convertDateString(p) { return (p < 10) ? '0' + p : p; }

  var startDate = new Date($(this).val());
  startDate.setDate(startDate.getDate() + interval);
    $('#end-date-picker').val(convertDateString(startDate.getMonth() + 1) + '/' + convertDateString(startDate.getDate()) + '/' + startDate.getFullYear());
});

JS Fiddle Demo JS小提琴演示

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

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