2016-03-19 71 views
4

我有一個帶停止按鈕的無限循環的Javascript計時事件。將秒轉換爲天,小時,分和秒

它將顯示數字時啓動按鈕是click.Now我想要這個號碼轉換爲像4小時後,3分鐘,50秒

HTML

<p class="start">Start</p> 
    <p class="end">End</p> 
    <p class="hide_div"> 
    <input type="text" id="txt" />//display numbers eg 12345 
</p> 

CSS

.hide_div { 
     display: none; 
    } 
    .end { 
     display: none; 
    } 

js

<script> 
    $(".start").on("click", function() { 
     //var start = $.now(); 
     //alert(start); 
     //console.log(start); 
     doTimer(); 
     $(".end").show(); 
     $(".hide_div").show(); 
    }); 
    $(".end").on("click", function() { 
     stopCount(); 
    }); 
</script> 
<script> 
    var c=0; 
    var t; 
    var timer_is_on=0; 

    function timedCount() 
    { 
     document.getElementById('txt').value=c; 
     c=c+1; 
     t=setTimeout(function(){timedCount()},1000); 
    } 

    function doTimer() 
    { 
     if (!timer_is_on) 
      { 
      timer_is_on=1; 
      timedCount(); 
     } 
    } 

    function stopCount() 
    { 
     clearTimeout(t); 
     timer_is_on=0; 

    } 

如何把123456這樣的數字轉換爲1天,4小時,40分鐘,45秒?

+0

您解決了這個問題? – C2486

回答

0

你可能會發現使用劃時代的時間戳更直截了當:由於這裏詳述Convert a Unix timestamp to time in JavaScript,基本方法是,像這樣:

    <script> 

        // Create a new JavaScript Date object based on the timestamp 
          // multiplied by 1000 so that the argument is in milliseconds, not seconds. 
          var date1 = new Date(); 

          alert ('easy trick to waste a few seconds...' + date1); 


          // var date = date2 - date1; 

          // Hours part from the timestamp 
          var hours1 = date1.getHours(); 
          // Minutes part from the timestamp 
          var minutes1 = "0" + date1.getMinutes(); 
          // Seconds part from the timestamp 
          var seconds1 = "0" + date1.getSeconds(); 



          var date2 = new Date(); 

          // Hours part from the timestamp 
          var hours2 = date2.getHours(); 
          // Minutes part from the timestamp 
          var minutes2 = "0" + date2.getMinutes(); 
          // Seconds part from the timestamp 
          var seconds2 = "0" + date2.getSeconds(); 



          // Will display time in 10:30:23 format 
          // var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2); 


          var elapsedHrs = hours2 - hours1; 
          var elapsedMin = minutes2.substr(-2) -minutes1.substr(-2); 
          var elapsedSec = seconds2.substr(-2) - seconds1.substr(-2);         

          var elapsedTime = elapsedHrs + ' hours, ' + elapsedMin + ' minutes, ' + elapsedSec + ' seconds'; 

          alert ('time between timestamps: ' + elapsedTime); 

        </script> 

但是要注意,這個腳本需要一些工作,因爲現在它會給負值例如date1 = 12:00:00和date2 = 12:00:05,但我現在就把它留給你。 你應該重寫你的代碼,在你的計時器開始時取一個時間戳(var x = new Date();),每當你完成/想要檢查經過的時間時取一個,並且在解析經過的秒數,分鐘,小時等按需要。

7

使用Math像這樣,在parseInt第二個參數是爲基數,這是可選

var seconds = parseInt(123456, 10); 
 

 
var days = Math.floor(seconds/(3600*24)); 
 
seconds -= days*3600*24; 
 
var hrs = Math.floor(seconds/3600); 
 
seconds -= hrs*3600; 
 
var mnts = Math.floor(seconds/60); 
 
seconds -= mnts*60; 
 
console.log(days+" days, "+hrs+" Hrs, "+mnts+" Minutes, "+seconds+" Seconds");

你GIVEN秒1234561 days, 10 Hrs, 17 Minutes, 36 Seconds1 days, 4 Hrs, 40 Minutes, 45 Seconds

相關問題