我創建了三個提示框來輸入日期,月份和日期。我試圖顯示出生年份,下一個轉折年,並顯示下一個生日的幾天,幾小時,幾分鐘和幾秒鐘。爲此我創建了日期對象。計算一切,但我沒有得到答案。任何人都可以幫我嗎?Javascript:計算出生年份
代碼:
<!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>
任何人都可以請幫助? – pari