jQuery UI 日期选择器:将 6 个月添加到另一个日期选择器 [英] jQuery UI datepicker: add 6 months to another datepicker

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

问题描述

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

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

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

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

这是我的代码:

$(".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"添加到第一个日期并将其作为默认日期发送到第二个.

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. 将选定的日期字符串解析为 JavaScript 日期对象.
  2. 使用Date.getMonth()Date.setMonth() 来更改月份.如有必要,后一个函数会自动递增/递减年份.
  3. 使用 jQuery datepicker 的 setDate 方法更改第二个日期选择器的日期(设置 defaultDate 不会给你想要的结果).
  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);
}

此处演示

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

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