Funcao adicionar Anos ao campo date Javascript

Asked

Viewed 127 times

1

I need a Javascript code that adds years to the selected date

function calculaData() {
var datainicial = document.getElementById("dataselecionada").value;
var partes = datainicial.split("-");
var ano = partes[0];
var mes = partes[1]-1;
var dia = partes[2];

datainicial = new Date(ano,mes,dia);
datafinal = new Date(datainicial);

//quantidade de dias
datafinal.setDate(datafinal.getDate() + 1);

var dd = ("0" + datafinal.getDate()).slice(-2);
var mm = ("0" + (datafinal.getMonth()+1)).slice(-2);
var y = datafinal.getFullYear();

//var dataformatada = dd + '/' + mm + '/' + y;
var dataformatada = y + '-' + mm + '-' + dd;
document.getElementById('dataAtualizada').value = dataformatada;

}
<label>Data</label>
<input type="date" id="dataselecionada" value="2018-03-10">
<br>
<p>Após adicionar um ano aparce o resultado abaixo no campo date</p>
<label>Data atualizada</label>
<input type="date" id="dataAtualizada">
<p>Exemplo 10/03/2019</p>

<input type="button" onclick="calculaData()" value="Calcular">

summary: pick date return in the field date updated date with the sum of one year. I managed with days but can not add year.

1 answer

2


Do as below:

function addZeroAesqueda(texto, tamanhoMaximo){
  texto = texto.toString();
  while (texto.length < tamanhoMaximo){
     texto = "0"+texto;
  }
 return texto;
}

document.getElementById('dataselecionada').onchange =function(e){
  let dataDigitada = e.target.value;
  let dataDigitadaSplit = dataDigitada.split("/");
  console.log('dataDigitada',dataDigitada)
  
  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;
  
  let data = new Date(ano, mes, dia);
  data.setFullYear(data.getFullYear()+1)
  
  ano= addZeroAesqueda(data.getFullYear(), 4);
  mes= addZeroAesqueda(data.getMonth()+1, 2);
  dia= addZeroAesqueda(data.getDate(), 2);
  
       
  document.getElementById('dataAtualizada').value = ano+'-'+mes+'-'+dia ;
};
<label>Data</label>
<input type="text" id="dataselecionada" value="10/03/18">
<br>
<p>Após adicionar um ano aparce o resultado abaixo no campo date</p>
<label>Data atualizada</label>
<input type="date" id="dataAtualizada">
<p>Exemplo 10/03/2019</p>

  • If the date typed is "02/29/16", the result will be "2017-02-29", but 2017 is not leap year, then 02/29 is an invalid date this year. Maybe it’s better to adapt the answers of this question, or use a lib like the Moment js., who already does the math by worrying about all these details. Calculations with dates is more complicated than it seems, and more "simple" approaches (and I would even say "naive") may even "work" for many cases, but there are many exceptions and special cases to consider.

Browser other questions tagged

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