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
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
Share Improve this question asked Jan 9, 2013 at 8:51 ap.singhap.singh 1,1604 gold badges15 silver badges35 bronze badges 1- htmlgoodies.com/html5/javascript/… – 4b0 Commented Jan 9, 2013 at 9:06
4 Answers
Reset to default 12function 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.
UPDATE:
MomentJS is not recommended anymore by their own declaration.
But just in case someone is curious about the solution using moment, this will work:
moment().diff(moment('2000'), 'years', 1) < 13
This will return false obviously, this is 2024 and the question was in 2013.
// 1. Get current date
// 2. Add 13 to the year in DOB.
// 3. check if current year is less than dob-year. If not person is older than 13 years
// 4. And so on check for month and date
var date_of_birth = '2015-12-26' // example DOB
var is_13 = true; // flag for 13
dob = date_of_birth.trim(); // trim spaces form DOB
y = parseInt(dob.substr(0,4)); // fetch year using substr() from DOB
m = parseInt(dob.substr(5,2)); // fetch month using substr() from DOB
d = parseInt(dob.substr(8,2)); // fetch date using substr() from DOB
// the above logic can change depending on the format of the input DOB
var r = new Date(); // Gets current Date
if(r.getFullYear() <= (parseInt(y) + 13)){
if((r.getMonth()+1) <= m){
if(r.getDate() < d){
is_13 = false;
console.log('less than 13');
}}}
Use
// millisecs * secs * mins * hrs * days (inclu. leap)
msecsInYear = 1000 * 60 * 60 * 24 * 365.25;
Note: Statically not correct, but is mathematically correct!
Will cover leap year cases.