Javascript jQuery UI 日期选择器:将 6 个月添加到另一个日期选择器

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

jQuery UI datepicker: add 6 months to another datepicker

javascriptjqueryjquery-uidatejquery-ui-datepicker

提问by Ben Totoro

I have two datepickers calendars, one for a start date, and another for an end date.

我有两个日期选择器日历,一个用于开始日期,另一个用于结束日期。

What I want is to set dynamically the defaultDate of the second datepicker to be six months later than the first one, when the first date is picked.

我想要的是在选择第一个日期时将第二个日期选择器的 defaultDate 动态设置为比第一个晚六个月。

I know how to report the fisrt date to the second datepicker, but I don't know how to add six months to the first then add it as the defaultdate of the second datepicker

我知道如何将第一个日期报告给第二个日期选择器,但我不知道如何将六个月添加到第一个,然后将其添加为第二个日期选择器的默认日期

Here is my code :

这是我的代码:

$(".firstcal").datepicker({
    dateFormat: "dd/mm/yy",
    onSelect: function (dateText, inst) {
        var date = $.datepicker.parseDate('dd/mm/yy', dateText);
        var $sec_date = $(".secondcal");
        $sec_date.datepicker("option", "defaultDate", date);
    }
});
$(".secondcal").datepicker({
    dateFormat: "dd/mm/yy"
});

Thanks a lot for your help

非常感谢你的帮助

Edit:

编辑:

In datePicker, the function to add six month to a date exists : it's labeled "+6M". I just want to add "+6M" to the first date and send it as the default date to the second.

在 datePicker 中,存在将六个月添加到日期的函数:它被标记为“+6M”。我只想将“+6M”添加到第一个日期并将其作为默认日期发送到第二个。

回答by Salman A

  1. Parse the selected date string into a JavaScript date object.
  2. Use Date.getMonth()and Date.setMonth()to change the month. The latter function automatically increments/decrements the year if necessary.
  3. Use jQuery datepicker's setDatemethod to change the date of the second datepicker (setting the defaultDatewill not give you the desired results).
  1. 将选定的日期字符串解析为 JavaScript 日期对象。
  2. 使用Date.getMonth()Date.setMonth()更改月份。如有必要,后一个函数会自动递增/递减年份。
  3. 使用 jQuery datepicker 的setDate方法来更改第二个 datepicker 的日期(设置defaultDate不会给你想要的结果)。
onSelect: function(dateText, instance) {
    date = $.datepicker.parseDate(instance.settings.dateFormat, dateText, instance.settings);
    date.setMonth(date.getMonth() + 6);
    $(".secondcal").datepicker("setDate", date);
}

Demo here

演示在这里

回答by Tank

To add 6 months to a date

将 6 个月添加到日期

 var second_date = new Date(date);
 second_date.setMonth(second_date.getMonth()+6); //+6 is however many months

then update the value

然后更新值

$("#secondcal").val(second_date);  //It is probably better to work with ID

回答by Matt

Here is the code i use to set the max date of a second dialog to one month later than the first. So you would just set default date instead

这是我用来将第二个对话框的最大日期设置为比第一个晚一个月的代码。所以你只需设置默认日期

   var beginsDate=$('#dlg_begins').datepicker('getDate');
   var monthMillisec=30*24*60*60*1000;
   var maxDate=new Date();
   maxDate.setTime( beginsDate.getTime() + monthMillisec );

   $('#dlg_expires').datepicker('option',
   {
       'maxDate':maxDate,
       'minDate':beginsDate
   });