Can anyone please guide me through javascript function, which will fetch me year difference between current date and date entered by user
I have tried this, but it doesnot keep in count leap years
var yearOld=2000
var dateOld=11
var mnthOld=1;
var currendDate=new Date(); // 9 - jan - 2013
var oldDate=new Date(yearOld,mnthOld-1,dateOld);
var timeDiff =currendDate-oldDate ;
var diffDays = timeDiff / (1000 * 3600 * 24 * 365);
Result is coming 13.00789 something where it should come less than 13
Any help will be appreciated
function getAge(birthDateString) {
var today = new Date();
var birthDate = new Date(birthDateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
if(getAge("27/06/1989") >= 18) {
alert("You have 18 or more years old!");
}
If you have good amount of Date related operations, consider using MomentJS http://momentjs.com/ . Check the first two examples there and i guess they fit your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With