0

I'm using datepicker to set the end date to be 6 months after the start date. What I have is working except in edge cases. Example. If start date is 31-Mar-2014 then the end date should be 30-Sep-2014. However I get 01-Oct-2014. I've read about this being a known issue in other thread comments Here and Here.

Is there something in JQuery or datepicker or an easy way that I can correct the date in edge cases like this?

There is likely an answer to this already but I haven't been able to scout it out. Any help would be greatly appreciated.

Here is the stripped down code and a demo I made that shows the example.

$("#StartDate").datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: "dd-M-yy",
    onSelect: function () {
        autoFillEndDate(); 
        }
 });
 $("#EndDate").datepicker({
     changeMonth: true,
     changeYear: true,
     dateFormat: "dd-M-yy"
 });

function autoFillEndDate() {
    if ($('#StartDate').val().length !== 0) {

        var offset;
        var offsetType;
        var StartDate = $('#StartDate').datepicker('getDate');

        var newDate = StartDate.setMonth(StartDate.getMonth() + 6);
        var EndDT = $.datepicker.formatDate('dd-M-yy', new Date(newDate));

        $('#EndDate').datepicker("setDate",EndDT);
    }
}
Community
  • 1
  • 1
bratak
  • 80
  • 13

1 Answers1

1

Have you seen this answer ?

Updated your fiddle with that given function and it gets 30-sep:

http://jsfiddle.net/z3yshuzc/1/

function addMonths(dateObj, num) {

    var currentMonth = dateObj.getMonth() + dateObj.getFullYear() * 12;
    dateObj.setMonth(dateObj.getMonth() + num);
    var diff = dateObj.getMonth() + dateObj.getFullYear() * 12 - currentMonth;

    // If don't get the right number, set date to 
    // last day of previous month
    if (diff != num) {
        dateObj.setDate(0);
    } 
    return dateObj;
} 
Community
  • 1
  • 1
artm
  • 8,554
  • 3
  • 26
  • 43