Add days to a javascript date [duplicate]

-1

Hello, I need help in a function that takes the date from a date field with the number of days and displays the new date as follows:

<label>Data</label>
<input type="date" id="data" value="2018-10-10">
<br>
<label>Dias</label>
<input type="text" id="dias" value="5">
<br>
<label>Data Somada</label>
<input type="text" id="data" value="15/10/2018">
<br>
    
asked by anonymous 13.12.2018 / 13:47

1 answer

1

<input name="data" type="date" id="data"  />


<input onchange="calculaDataFin();" name="dias" type="number" id="dias" size="70"  />

<input name="datafin" type="date" id="datafin"  readonly/>


<input type="button" onclick="calculaDataFin()">

<script type="text/javascript">

function calculaDataFin() {
var datainicial = document.getElementById("data").value;
var dias = parseInt(document.getElementById("dias").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);
datafinal.setDate(datafinal.getDate() + dias);

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

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

}

</script>

I found on the internet what I need! Thanks for the help.

    
13.12.2018 / 14:10