在javascript中检查年龄是否不低于13岁

ap.*_*ngh 4 javascript

任何人都可以通过javascript函数指导我,这将获取用户输入的当前日期和日期之间的年份差异

我试过这个,但它并没有计入闰年

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); 
Run Code Online (Sandbox Code Playgroud)

结果是13.00789,它应该低于13

任何帮助将不胜感激

Daw*_*dak 7

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!");
} 
Run Code Online (Sandbox Code Playgroud)


Mah*_*bub 5

如果您有大量与日期相关的操作,请考虑使用MomentJS http://momentjs.com/.检查那里的前两个例子,我猜它们适合你的问题.