Handling of Javascript Dates

Asked

Viewed 108 times

0

I need to take a date of an input and add a few years to it and present the result on another label.

Ex = 01/01/2017 - User Selected Date 01/01/2020 - Date calculated by js

  • You want to solve the problem with Javascript or with C#?

2 answers

0

<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>

0

A 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)

So can be changed either year, month or date day according to need.

Browser other questions tagged

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