html计算年龄代码,从“日期”计算年龄HTML元素
我有一個帶有“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:
總結
以上是生活随笔為你收集整理的html计算年龄代码,从“日期”计算年龄HTML元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英语句子成分分析(四)
- 下一篇: HTML无序列表布局