I want to calculate age of anything.i already wrote the program. I took a date object and assigned value into it $birth_date=new Date('Dec,15,1992');
then i subtract the value with the current data.According to the subtraction it should returns 23 years 0 months 14 days.but it returns 23 years 4 months 14 days.years and days are OK but the month misleads.
would you pls tell me why showing this weird results?
following code is HTML and next to Javascript
$(document).ready(function(){
var birth_date = new Date('Dec, 15, 1992').getTime();
var years,months,days, hours, minutes, seconds;
var ageCount = document.getElementById('counter');
setInterval(function(){
var current_date = new Date().getTime();
var total_sec =(current_date-birth_date) / 1000;
years=parseInt(total_sec/(86400*30*12));
var second_left=total_sec%(86400*30*12);
months=parseInt(second_left/(86400*30));
second_left=second_left%(86400*30);
days=parseInt(second_left/86400);
second_left=second_left%(86400);
hours=parseInt(second_left/3600);
second_left=second_left%3600;
minutes=parseInt(second_left/60);
seconds=parseInt(second_left%60);
ageCount.innerHTML=years+' Years '+months+' Months '+days+' Days '+hours+
' Hours '+minutes+' Minutes '+seconds+' Seconds';
},500);
});
<div id="counter">
</div>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>