我想过滤所有可以在我的网站注册的用户.如何使用允许18岁及以上的jquery过滤注册人的年龄,但是当年龄为13至17岁时,他们可以注册,但他们必须选中复选框以获得家长同意.我正在使用mm/dd/yyyy格式的文本框.
我的第一个想法:
用法:
var age = getAge(new Date(1984, 7, 31));
Run Code Online (Sandbox Code Playgroud)
function getAge(birthDate) {
var now = new Date();
function isLeap(year) {
return (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0));
}
// days since the birthdate
var days = Math.floor((now.getTime() - birthDate.getTime())/1000/60/60/24);
var age = 0;
// iterate the years
for (var y = birthDate.getFullYear(); y <= now.getFullYear(); y++){
var daysInYear = isLeap(y) ? 366 : 365;
if (days >= daysInYear){
days -= daysInYear;
age++;
// increment the age only if there are available enough days for the year.
}
}
return age;
}
Run Code Online (Sandbox Code Playgroud)