I need to get a date from an input and add a few years in it and display the result in another label.
Ex = 01/01/2017 - User Selected Date 01/01/2020 - Date computed by js
I need to get a date from an input and add a few years in it and display the result in another label.
Ex = 01/01/2017 - User Selected Date 01/01/2020 - Date computed by js
One way to do this is as follows:
var d = new Date(2017,0,1);
var year = d.getFullYear();
var month = d.getMonth();
var day = d.getDate();
var c = new Date(year + 3, month, day)
// => Wed Jan 01 2020 00:00:00 GMT-0300 (Hora Padrão de Buenos Aires)
This can change both the year, month or day of the date as needed.
<input type="text" id="ini" value="28/03/2017"><br>
<input type="text" id="fim"><br>
<script>
var anos = 3;
var dataNoCampo = document.getElementById('ini').value.split('/');
var ini = new Date(dataNoCampo[2],dataNoCampo[1],dataNoCampo[0]);
var fim = new Date(ini.getTime() + (anos * 24 * 60 * 60 * 1000 * 365));
document.getElementById('fim').value = fim.getDate() + "/" + fim.getMonth() + "/" + fim.getFullYear();
</script>