jQuery UI datepicker:添加6个月到另一个datepicker [英] jQuery UI datepicker: add 6 months to another datepicker

查看:118
本文介绍了jQuery UI datepicker:添加6个月到另一个datepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个日期戳记日历,一个是开始日期,另一个是结束日期。



我想要的是动态设置第二个datepicker的defaultDate比起第一个日期晚六个月,当第一个日期被选择。



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



这是我的代码:



pre $ $(。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
});

非常感谢您的帮助



编辑:



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

解决方案


  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);
}

在此演示


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.

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.

解决方案

  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 setDate method to change the date of the second datepicker (setting the defaultDate will not give you the desired results).

onSelect: function(dateText, instance) {
    date = $.datepicker.parseDate(instance.settings.dateFormat, dateText, instance.settings);
    date.setMonth(date.getMonth() + 6);
    $(".secondcal").datepicker("setDate", date);
}

Demo here

这篇关于jQuery UI datepicker:添加6个月到另一个datepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆