似乎无法找到问题所在。我每次运行它都会获得ageDale
的NaN,现在已经看了一会儿,它可能很简单,但我很感激帮助!
<p>Enter names in the fields, then click "Submit" to submit the form:</p>
<form name="form">
<input type="text" id="birthDate">
Current Date
<input type="text" id="currentDate">
<a id="Submit_Button" onclick="test();" href="javascript:void(0);" title="Submit">Submit</a>
</form>
<script>
function test() {
var birthDate = document.getElementById("birthDate");
var currentDate = document.getElementById("currentDate");
var ageDate = (birthDate.value - currentDate.value);
if(ageDate.value < 1) {
(ageDale = ageDate.value * -1)
}
else {
(ageDale = ageDate.value * 1)
}
alert(ageDale)
}
</script>
另外,我是否有必要发表其他声明?还是有其他方法来设置它,所以不需要它?
答案 0 :(得分:2)
这个
ageDate.value
应该是
ageDate
只。它是一个变量,只包含与
的差异birthDate.value - currentDate.value
if(ageDate.value < 1) {
// ^ here
(ageDale = ageDate.value * -1)
} //^ here
else {
(ageDale = ageDate.value * 1)
// ^ and here
您只需从获取数据时获取值,例如输入字段。
另外(取决于您输入它们的方式),计算日期可能会有问题。出于调试目的,你应该
console.log()
您的变量值,这样您就可以快速找到错误的位置。
代码中的console.log()的好地方就是,例如在这个块之后:
var birthDate = document.getElementById("birthDate");
var currentDate = document.getElementById("currentDate");
var ageDate = (birthDate.value - currentDate.value);
console.log(ageDate);
旁注:
您可能需要查看moment.js,它将帮助您进行日期计算。例如,您可以使用moment.js获取日期之间的差异:
var a = moment([2014, 12, 05]);
var b = moment([2014, 12, 06]);
a.diff(b, 'days') // 1
答案 1 :(得分:1)
试试这个:
var btn = document.getElementById("Submit_Button");
btn.onclick = function test() {
var birthDate = parseInt(document.getElementById("birthDate").value);
var currentDate = parseInt(document.getElementById("currentDate").value);
var ageDate = (birthDate - currentDate);
if(ageDate < 1) {
(ageDate = ageDate * -1)
}
else {
(ageDate = ageDate * 1)
}
alert(ageDate)
}
答案 2 :(得分:1)
正如baao所说,你有拼写错误。在纠正这些之后,您需要考虑输入的内容,并确保检查输入是否有效。
例如,如果我输入&#34; 9月10日&#34;我的生日和#34; 12月10日&#34;对于当前日期,您的函数将尝试减去两个无效的字符串。如果您要在日期中使用自定义输入字段,则需要确保其格式一致且可解析。
我建议以特定格式询问他们的生日并从那里解析,因为我们可以使用Javascript轻松获取当前日期。例如,mm-dd-yy。我们可以将其重写为:
function test() {
//lets get the date, in the format 'mm-dd-yy'. You'd want to do error checking at some point if you're serious about it
var dateInput = document.getElementById("birthDate").value;
//get each individal date type by splitting them at the -, giving ['dd', 'mm', 'yy']
var dateSplit = dateInput.split('-');
//create a Javascript date object with the date we got
var birthDate = new Date(dateSplit[2], dateSplit[0], dateSplit[1]);
//create another with the current date and time
var currentDate = new Date();
// find the difference in milliseconds
var dateDifference = Math.abs(birthDate.getTime() - currentDate.getTime());
// convert to years
var age = dateDifference / (1000 * 3600 * 24 * 365);
alert(age);
}
&#13;
<p>Enter names in the fields, then click "Submit" to submit the form:</p>
<form name="form">
Birth Date (dd-mm-yy):
<br>
<input type="text" id="birthDate">
<br>
<a id="Submit_Button" onclick="test();" href="javascript:void(0);" title="Submit">Submit</a>
</form>
&#13;
答案 3 :(得分:0)
只需修改此代码
var birthDate = document.getElementById("birthDate");
var currentDate = document.getElementById("currentDate");
var ageDate = (birthDate.value - currentDate.value);
if(ageDate.value < 1) {
(ageDale = ageDate.value * -1)
}
else {
(ageDale = ageDate.value * 1)
}
用这个
var vbirthdate = new Date(document.getElementById("birthDate").value);
var vcurrentdate = new Date(document.getElementById("currentDate").value);
var ageDate = Math.floor((vbirthdate-vcurrentdate)/(1000*60*60*24));
if(ageDate < 1) {
(ageDate = ageDate * -1)
} // no need to do something like this (ageDate *1) if it is already a positive number, just check if it's a negative then convert it to a positive number
您可以尝试http://jsfiddle.net/kapasaja/duco4cqa/5/
处的代码你问的是什么类似于this post