Skip to content Skip to sidebar Skip to footer

Jquery: Add Months To Datepicker

With the help of jQuery and Datepicker, I would like to add # of months to Today's Date. My attempts below produce unexpected results: e.g. If I add 0 months, I get 9 months additi

Solution 1:

Your problem is that ten is a string, not an integer, so when you execute

d.getMonth() + ten

you get something like 112 instead of 13. Change

var ten = $('#mthschange').val();

to

var ten = parseInt($('#mthschange').val());

and the code works fine:

$(function() {
  $('#mthschange').change(function() {
    var ten = parseInt($('#mthschange').val());
    var d = newDate();
    d.setMonth(d.getMonth() + ten);
    var e = ($.datepicker.formatDate('yy-mm-dd', d));
    $("#res").val(e);
  });
});
<!doctype html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><title>jQuery UI Datepicker - Default functionality</title><linkrel="stylesheet"href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"><linkrel="stylesheet"href="/resources/demos/style.css"><scriptsrc="https://code.jquery.com/jquery-1.12.4.js"></script><scriptsrc="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script></head><body><p>Input Field : <inputid="mthschange"type="text"></p><p>Output Field : <inputid="res"type="text"></p></body></html>

Post a Comment for "Jquery: Add Months To Datepicker"