2
I need to calculate the difference between two dates reported. I need to know the results in years and months and days. I used the momentjs library in conjunction with Angularjs.
By setting the start date as 06/07/2015 and the date of birth as 06/07/1957, the expected result would be 58 years 0 months and 0 days. But the result was 58 years 0 months and 6 days
The JS function that does this conversion is as follows:
function converterData(){
var hoje = moment($scope.final, "DD/MM/YYYY");
var dia = moment($scope.nascimento, "DD/MM/YYYY");
var duracao = moment.duration(hoje.valueOf()- dia.valueOf(), 'milliseconds');
$scope.idadeAnos = duracao.years();
$scope.idadeMeses = duracao.months();
$scope.idadeDias = duracao.days();
}
I couldn’t find the mistake!
what is the result of
hoje.valueOf()
and ofdia.valueOf()
?– paulojean
end: 1436151600000 birth: -394146000000
– Israel Zebulon
You can see a different result on this site:http://www.profcardy.com/calculated/applicationsphp?calc=5
– Israel Zebulon
Dude, I ran some tests, and although I couldn’t identify why, I saw that the problem occurs when you try to make a difference with the day beating. A workaround would you add a day in
hoje
, take the duration and, before passing the duration values, doduracao = duracao.subtract(1,'days')
(is ugly, but it was the alternative I thought)– paulojean