2017-10-14 86 views
0

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

任何人都可以請幫助? – pari

回答

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>

+0

你真的瞭解**你的**代碼嗎?什麼是'secondsBetween'變量? –

+0

這只是我爲了計算出生年份而給出的變量名稱。我改了它的名字。 – pari

+0

你能告訴我如何讓這段代碼工作嗎? – pari