2014-09-24 116 views
0

我不得不創建拍賣風格倒計時器。我在JavaScript代碼中預設了結束日期,然後我必須弄清楚如何計算差異。我的代碼工作正常,但我希望看看是否有這樣做的有效方法。我從書中得到了日計算,但認爲沒有必要創建一個小時,分鐘,秒變量並且除以它。Javascript日期和時間倒計時

var day = 1000 * 60 * 60 *24; 

// Initialize and set variables. 
var startDate = new Date(); 
startDate.toTimeString(); 
var endDate = new Date('4 Oct 2014 12:30:00 UTC'); 
endDate.toTimeString(); 
var startHour = startDate.getHours(); 
var startMin = startDate.getMinutes(); 
var startSec = startDate.getSeconds(); 
var endHour = endDate.getHours(); 
var endMin = endDate.getMinutes(); 
var endSec = endDate.getSeconds(); 


// Make sure the start date comes first: 
if (startDate < endDate) { 

    // Get the interval: 
    var diffDay = endDate - startDate; 

    if (diffDay <= day) { 
     intervalD = '1 day '; 
    } else { 
     intervalD = Math.round(diffDay/day) + ' days '; 
    } 

    if (endMin >= startMin){ // Make sure its counting down 
     var diffHour = Math.abs(endHour - startHour); 
    } else { //Since start hour is greater, we now have to count down from 60. 
     var diffHour = ((endHour - startHour) + 60); 
    } 


    if (endMin >= startMin){ 
     var diffMin = Math.abs(endMin - startMin); 
    } else { 
     var diffMin = ((endMin - startMin) + 60); 
    } 

    if (endSec >= startSec){ 
     var diffSec = Math.abs(endSec - startSec); 
    } else { 
     var diffSec = ((endSec - startSec) + 60); 
    } 
+0

如果您想要某人查看您的代碼,您應該詢問[codereview.se] – 2014-09-24 03:35:34

+0

非常感謝您的提示! – EsotericRider 2014-09-24 05:10:17

回答

0

請在你的文件中填寫下面的代碼並應用期望的日期。這對我來說可以。如果有任何問題,請告知我。

<body> 
    <form name="count"> 
    <input type="text" size="69" name="count2"> 
    </form> 
    <script type="text/javascript"> 

     //change the text below to reflect your own, 
     var before = "Christmas!" 
     var current = "Today is Christmas. Merry Christmas!" 
     var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec") 

     function countdown(yr, m, d) { 
      theyear = yr; themonth = m; theday = d 
      var today = new Date() 
      var todayy = today.getYear() 
      if (todayy < 1000) 
       todayy += 1900 
      var todaym = today.getMonth() 
      var todayd = today.getDate() 
      var todayh = today.getHours() 
      var todaymin = today.getMinutes() 
      var todaysec = today.getSeconds() 
      var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec 
      futurestring = montharray[m - 1] + " " + d + ", " + yr 
      dd = Date.parse(futurestring) - Date.parse(todaystring) 
      dday = Math.floor(dd/(60 * 60 * 1000 * 24) * 1) 
      dhour = Math.floor((dd % (60 * 60 * 1000 * 24))/(60 * 60 * 1000) * 1) 
      dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000))/(60 * 1000) * 1) 
      dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000))/1000 * 1) 
      if (dday == 0 && dhour == 0 && dmin == 0 && dsec == 1) { 
       document.forms.count.count2.value = current 
       return 
      } 
      else 
       document.forms.count.count2.value = "Only " + dday + " days, " + dhour + " hours, " + dmin + " minutes, and " + dsec + " seconds left until " + before 
      setTimeout("countdown(theyear,themonth,theday)", 1000) 
     } 

     //enter the Future count down date using the format year/month/day 
     countdown(2016, 9, 24) 
    </script> 
</body> 
+0

不是我正在尋找,謝謝 – EsotericRider 2014-09-24 04:25:00