Through two input='text'
I launch the start date and the end date. By JavaScript I want to subtract the end date by the initial and return the amount of years, months and days. I am using the Date()
function, but I am able to return only the number of days. By function Date()
can I 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">