将秒数转换为天,小时,分钟和秒 [英] Convert seconds to days, hours, minutes and seconds

查看:267
本文介绍了将秒数转换为天,小时,分钟和秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有停止按钮的无限循环的Javascript计时事件。

I have a Javascript timing event with an infinite loop with a stop button.

当点击开始按钮时它会显示数字。现在我想要将这些数字转换为4小时3分50秒

It will display numbers when start button is click.Now I want this numbers converted to something like 4 hours, 3 minutes , 50 seconds

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;

}

$(".start").on("click", function() {
  //var start = $.now();
  //alert(start);
  //console.log(start);
  doTimer();
  $(".end").show();
  $(".hide_div").show();
});
$(".end").on("click", function() {
  stopCount();
});

.hide_div {
  display: none;
}

.end {
  display: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

如何将123456等数字转换为1天,4小时,40分钟,45秒?

How to convert numbers like 123456 to 1 day, 4 hours, 40 min, 45 seconds?

推荐答案

像这样使用 Math parseInt 中的第二个参数用于base,这是可选的

Use Math like this way, Second param in parseInt is for base, which is optional

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

您的给定秒数 123456 1天,10小时,17分钟,36秒不是 1天,4小时,40分钟,45秒

这篇关于将秒数转换为天,小时,分钟和秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆