我正在尝试开发儿童杂志网站并为儿童制作表格,在这种表格中,我试图将输入内容作为(日期)类型并按年计算差异,并返回消息或警报(如果年龄是18 = <年龄<= 6,现在我已经尝试了许多在本网站或其他网站中找到的代码,例如: 1. javascript - Age calculation (我试过:getTime()和其他代码) 2. https://www.w3resource.com/javascript-exercises/javascript-date-exercise-18.php 而且很多人都有相同的想法,但是都没有用..
这是代码:
</head>
<body>
<input type="date" id="userage" name="userage" onchange=""/>
<p id="k" ></p>
<script>
var a = document.getElementById('k').value ;
var b = Date.parse(a) ;
var c = new Date(b);
var d = c.getFullYear();
function useragefun(birthday){
var e = new Date();
var f = e.getFullYear();
var g = Math.floor(f - birthday);
if (g >= 18 || g <= 6 ){
document.getElementById('k').innerHTML = "You better sign in the adults form";
}else "" ;
}
</script>
</body>
//可能是这个,也可能是警报。
答案 0 :(得分:0)
不确定自己想要什么,但这似乎就是
spinner
function useragefun(birthday){
let birthDate = new Date(birthday),
today = new Date(),
years = (today.getFullYear() - birthDate.getFullYear());
if (today.getMonth() < birthDate.getMonth() ||
today.getMonth() == birthDate.getMonth() && today.getDate() < birthDate.getDate()) {
years--;
}
if (years >= 18 || years <= 6 ){
document.getElementById('k').innerHTML = "You better sign in the adults form";
} else "";
}