我创建了三个提示框来输入日期,月份和日期。我正在尝试显示出生年份,下一个转折年份并显示下一个生日的天数,小时数,分钟数和秒数。为此,我创建了日期对象。计算一切,但我没有得到答案。有人可以帮帮我吗?
代码:
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
font-family: monospace;
font-size: 20px;
}
</style>
<title>lab14</title>
</head>
<body background= "lab14_images/birth.jpg">
<h1 style="text-align: center; font-family: monospace;"> My age </h1>
<p id="dateField"> </p>
<p id="birthField"> </p>
<p id="nextBirth"> </p>
<script>
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var today = new Date();
var startDate = new Date();
var myMonth = prompt("What month were you born in?");
var myDay = prompt("What day were you born on?");
var myYear = prompt("What year were you born in?");
var dateLoc = document.getElementById("dateField").innerHTML = "Today is " + today;
var birthField = document.getElementById("birthField")
.innerHTML = "I was born on " + monthNames[myMonth - 1] + ' ' + myDay + ',' + myYear + '.'
+ '(' + myMonth + '/' + myDay + '/' + myYear + ')';
var endDate = new Date(today.getFullYear() + 1, myMonth, myDay);
// !!! WHERE is startDate defined???
var s = (endDate.getTime() - startDate.getTime()) / 1000
var nextBirth = document.getElementById("nextBirth").innerHTML = "I am" + endDate + "Years old, and will turn"+ endDate + 1 +"in:"+ days +"days," + hours +"hours" + minutes +"minutes" + seconds +"seconds" ;
</script>
</body>
</html>
答案 0 :(得分:0)
我修正了1错误:在实际使用
之前,已将值的分配移至endDate
。
请查看我的!!!
评论。您正在使用既未定义也未初始化的变量。
使用代码段播放,并注意屏幕右下角的控制台错误。
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var today = new Date();
//var birth = new Date();
var myMonth = prompt("What month were you born in?");
var myDay = prompt("What day were you born on?");
var myYear = prompt("What year were you born in?");
var dateLoc = document.getElementById("dateField").innerHTML = "Today is " + today;
var birthField = document.getElementById("birthField")
.innerHTML = "I was born on " + monthNames[myMonth - 1] + ' ' + myDay + ',' + myYear + '.'
+ '(' + myMonth + '/' + myDay + '/' + myYear + ')';
var endDate = new Date(today.getFullYear() + 1, myMonth, myDay);
// !!! WHERE is startDate defined???
var secondsBetween = (endDate.getTime() - startDate.getTime()) / 1000
var seconds = Math.floor( (endDate/1000) % 60 );
var minutes = Math.floor( (endDate/1000/60) % 60 );
var hours = Math.floor( (endDate/(1000*60*60)) % 24 );
var days = Math.floor( endDate/(1000*60*60*24) );
var nextBirth = document.getElementById("nextBirth").innerHTML = "I am" + endDate + "Years old, and will turn"+ endDate + 1 +"in:"+ days +"days," + hours +"hours" + minutes +"minutes" + seconds +"seconds" ;
p{
text-align: center;
font-family: monospace;
font-size: 20px;
}
<h1 style="text-align: center; font-family: monospace;"> My age </h1>
<p id="dateField"> </p>
<p id="birthField"> </p>
<p id="nextBirth"> </p>