Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

javascript - jQuery UI datepicker: add 6 months to another datepicker

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...