最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

To check if age is not less than 13 years in javascript - Stack Overflow

programmeradmin4浏览0评论

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
Add a comment  | 

4 Answers 4

Reset to default 12
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.

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.

发布评论

评论列表(0)

  1. 暂无评论