Add a month in jQuery

1

I have the following date:

  

2017-12-22 (December 22, 2017);

I would need an increment that would add + 1 month to the current one, returning:

  

2018-01-22 (January 22, 2018).

How do I do this in jQuery?

    
asked by anonymous 04.10.2017 / 22:27

2 answers

6

var date = new Date();
console.log(date); // "2017-10-04T20:36:17.560Z"
date.setMonth(date.getMonth() + 1);
console.log(date); // "2017-11-04T21:36:17.560Z"

Use .getMonth() to know the month of the date, then use .setMonth() to change the month.

Then to show formatted can be for example like this:

var date = new Date();
console.log(date); // "2017-10-04T20:36:17.560Z"
date.setMonth(date.getMonth() + 1);
console.log(date); // "2017-11-04T21:36:17.560Z"

var formatadoA = date.toLocaleDateString('pt-BR');
console.log(formatadoA); // dá 04/11/2017

var formatadoB = [
  date.getDate(), date.getMonth() + 1, date.getFullYear()
].map(nr => nr < 10 ? '0' + nr : nr).join('-');
console.log(formatadoB); // dá 04-11-2017
    
04.10.2017 / 22:36
0

If you just add the month, you can do it as follows in the link

function calcularMes(){
    var dataIncial = $('#checkin').val();
    var dataArray = dataIncial.split('/');
    var dia = dataArray[0];
    var mes = parseInt(dataArray[1]) + 1;
    var ano = dataArray[2];

    var novaData = dia + "/"+ mes + "/"+ano;
    $('#checkout').val( novaData );

}

link

    
04.10.2017 / 23:16