4
Through two input='text'
I set the starting date and the final date. By Javascript I want to subtract the final date by the initial one and return the number of years, months and days. I’m using the function Date()
, but I can return only the number of days. By the function Date()
can return this value?
Javascript
var data_inicial = document.getElementById('data_inicial').value;
var data_final = document.getElementById('data_final').value;
if(data_inicial != '' && data_final != ''){
var date_admissao = new Date(data_inicial.substr(6,4), data_inicial.substr(3,2)-1, data_inicial.substr(0,2));
var date_demissao = new Date(data_final.substr(6,4), data_final.substr(3,2)-1, data_final.substr(0,2));
var dias_total = Math.ceil((date_demissao.getTime()-date_admissao.getTime())/1000/60/60/24);
alert(dias_total);
}
HTML
<input type="text" name="data_inicial" id="data_inicial">
<input type="text" name="data_final" id="data_final">
Why don’t you use the total days to have years, months and days?
– Felipe Avelar
@Erloncharles is not duplicate. Note that I say that subtraction works. The questioning is the return of this subtraction.
– Lucas Henrique
@Felipeavelar the
Date()
does not have this functionality?– Lucas Henrique
No, this should be done by hand, but are relatively simple calculations.
– Felipe Avelar
@Felipeavelar are simple if you ignore bisext/common years and if you ignore months with 28/30 or 31 days... otherwise they are not so simple...
– Sergio
Have you tried the atomic subtraction? Taking year, months and days. Even if the treatment is a little complicated, it should be easier to do than taking the total of days.
– Felipe Avelar
@Felipeavelar I tried, but it didn’t work.
– Lucas Henrique