我有一个带有“Date”元素和提交按钮的HTML表单。有2个功能。一个用于计算“日期”HTML元素中给定出生日期的年龄。另一个功能是比较年龄和最小允许年龄。我需要帮助来完成它。在代码中看到我的问题。是的,我见过其他类似的问题。我想学习,而不仅仅是复制/粘贴。如果我明白我的代码/语法有什么问题,那么我会更好地学习。提前致谢。从“日期”计算年龄HTML元素
var minAge = 18;
var curDate = new Date();
var curr_year = curDate.getFullYear();
var curr_month = curDate.getMonth();
//Calculates age from given Birth Date in the form//
function _calcAge() {
var dt1 = document.getElementById('date').value;
var birth_date = new Date(dt1);
var birth_year = birth_date.getFullYear();
var birth_month = birth_date.getMonth();
var calc_year = curr_year - birth_year;
var calc_month = curr_month - birth_month;
//The following below is what I am not sure about.
//I need to combine years and months and
//convert them into a string??? Is this syntax dead wrong?
var final_result = (calc_year && "." && calc_month).toString();
// final result should be a number with a decimal point, example: 35.5
final_result = parseFloat;
return (final_result);
alert(final_result);
}
//Compares calculated age with minimum age and acts according to rules//
function _setAge() {
var age = _calcAge();
//alert("my age is " + age);
if (age < minAge) {
alert("You are not allowed into the site. The minimum age is 18!");
} else
alert("Welcome to my Site");
window.open(main.htm, _self);
}
Date Of Birth: