2016-03-19 18 views
4

私は、ストップボタン付き無限ループのJavascriptタイミングイベントを持っています。秒を日、時間、分、秒に変換します。

スタートボタンが、私はこの数字は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; 
    } 
のようなものに変換したいclick.Nowあるときには数字が表示されます

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

使用MathparseIntで二paramはベース用で、オプションである

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");

あなたの与えられた秒1234561 days, 10 Hrs, 17 Minutes, 36 Secondsない1 days, 4 Hrs, 40 Minutes, 45 Seconds

関連する問題