Add months to a javascript date

Asked

Viewed 1,755 times

-1

I own a field that receives a date, from that date it should add up to 1 month

//Funcao das datas
function addData() {

//Pegar data Atual para somar
var currentDate = new Date();

//pegar data atual para exibir
var currentDate1 = new Date();

//Capturar Quantidade de meses
var meses = "1";
//Parse Int dos meses
var a = parseInt(meses);


//Adicionar meses 
currentDate.setMonth(currentDate.getMonth()+a);

//Trazer data Atual
currentDate1.setDate(currentDate1.getDate());

   

//Exibir data Atual
document.getElementById('data').value = currentDate1.toLocaleDateString();


//Exibir a data ja atualizada
document.getElementById('dataAtualizada').value = currentDate.toLocaleDateString();

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>Data</label>
<input type="text" id="data" value="">
<br>

<label>Data Atualizada</label>
<input type="text" id="dataAtualizada" value="">
<br>

<input type="button" onclick="addData()" value="Calc">

I took a code that adds but from the current date, I need it to capture the date that is in the "text" field and not the current one.

Example there is a "text" field with the following date Date 10/10/2018

Updated date 10/11/2018

1 answer

4


A function was created to convert the dates:

function parseDate(texto) {
  let dataDigitadaSplit = texto.split("/");

  let dia = dataDigitadaSplit[0];
  let mes = dataDigitadaSplit[1];
  let ano = dataDigitadaSplit[2];


  if (ano.length < 4 && parseInt(ano) < 50) {
    ano = "20" + ano;
  } else if (ano.length < 4 && parseInt(ano) >= 50) {
    ano = "19" + ano;
  }
  ano = parseInt(ano);
  mes = mes - 1;

  return new Date(ano, mes, dia);
}

//Funcao das datas
function addData() {

  var dataDigitada = document.getElementById('data').value;

  //Pegar data Atual para somar
  var currentDate =parseDate(dataDigitada);

  //pegar data atual para exibir
  var currentDate1 = new Date();

  //Capturar Quantidade de meses
  var meses = "1";
  //Parse Int dos meses
  var a = parseInt(meses);


  //Adicionar meses 
  currentDate.setMonth(currentDate.getMonth() + a);

  //Trazer data Atual
  currentDate1.setDate(currentDate1.getDate());



  //Exibir data Atual
  document.getElementById('data').value = currentDate1.toLocaleDateString();


  //Exibir a data ja atualizada
  document.getElementById('dataAtualizada').value = currentDate.toLocaleDateString();

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>Data</label>
<input type="text" id="data" value="">
<br>

<label>Data Atualizada</label>
<input type="text" id="dataAtualizada" value="">
<br>

<input type="button" onclick="addData()" value="Calc">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.