1

I want to select any date and convert it to same date of next upcoming month. For example- I select 1st jun 2014 then it show 1st july 2014 and so on..

Fiddle

HTML

<input type='text' id='txtDate' readonly='true' />
<input type='button' id='btnConvert' value='Convert' /><br/>
Current Date : <span id='spnCurrentDate'></span><br/>
Next Month Date : <span id='spnNewDate'></span>

JS

    $("#txtDate").datepicker({
              changeMonth: true
    });

    $("#btnConvert").click(function(){
    $("#spnCurrentDate").html($("#txtDate").val());
    $("#spnNewDate").html($("#txtDate").val());
kamesh
  • 2,374
  • 3
  • 25
  • 33
S. S. Rawat
  • 5,943
  • 4
  • 43
  • 59
  • possible duplicate of [jQuery UI datepicker: add 6 months to another datepicker](http://stackoverflow.com/questions/11137616/jquery-ui-datepicker-add-6-months-to-another-datepicker) – Nick R Jun 23 '14 at 08:49
  • 2
    Note that you can't simply add 1 to months - 2014-01-31 + 1 month -> 2014-02-31 which is converted to 2014-03-03. So if you roll over to the following month, you probably want to set the day to the last of the previous month. – RobG Jun 23 '14 at 08:53

3 Answers3

1

If you are just asking how to add one month to a Date object, you can't just add one to the month as 2014-01-31 + 1 month -> 2014-02-31 which is converted to 2014-03-03.

So if you roll over to the following month, you probably want to set the day to the last of the previous month:

function addOneMonth(date) {
  var o = new Date(+date);
  date.setMonth(date.getMonth() + 1);

  // If have rolled over an extra month, set to last
  // day of previous month
  if (date.getDate() != o.getDate()) {
   date.setDate(0);
  }
  return date;
}

addOneMonth(new Date(2014,0,31)); // 2014-02-28

or not…

RobG
  • 142,382
  • 31
  • 172
  • 209
0

DEMO here

 var date1 = new Date();
 date1.setMonth(date1 .getMonth() + 1);

now date1 is an object holding a date which is 1 months later

If you want to set it to the jQuery UI DatePickers, you can do this

$("#txtDate").datepicker({
    changeMonth: true
});

$("#btnConvert").click(function(){
    var date1 = new Date($("#txtDate").val());
    date1.setMonth(date1 .getMonth() + 1);

    $("#txtDate").datepicker("setDate", date1 );

});
Deep Sharma
  • 3,374
  • 3
  • 29
  • 49
-1

Try This Demo : http://jsfiddle.net/PQfDc/151/

$("#txtDate").datepicker({
    changeMonth: true,
    minDate:'0',
    onClose: function( selectedDate ) {
        var d = new Date(selectedDate)
        d.setMonth(d.getMonth() + 1);
        $("#txtDate").datepicker("setDate", d );
        $("#spnSelectedDate").text(selectedDate);
        $("#spnNewDate").text($('#txtDate').val());
    }
});


$("#txtDate1").datepicker({
    changeMonth: true,
    minDate:'0' 
});

$("#btnConvert").click(function(){
    var d1 = new Date($("#txtDate1").val());
    var date = d1.getDate();
    var month = d1.getMonth()+1; 
    var year = d1.getFullYear();
    var newDate = month + "/" + date + "/" + year    
    $("#spnSelectedDate1").text($("#txtDate1").val());
    $("#spnNewDate1").text(newDate);
});
Tejas Savaliya
  • 572
  • 7
  • 8