javascript 将刻度转换为时间格式 (hh:mm:ss)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14350148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:25:17  来源:igfitidea点击:

Convert ticks to time format (hh:mm:ss)

javascripthtmltime

提问by Null Pointer

I am getting a video length value as ticks from web server. I want to display it in a "hh:mm:ss" format. How can I do this in JavaScript?

我从网络服务器获取视频长度值作为刻度。我想以“hh:mm:ss”格式显示它。我怎样才能在 JavaScript 中做到这一点?

回答by nbrooks

Assuming that ticks are in seconds (you can convert them to seconds first if they aren't), you can get to the desired format by finding the number of whole minutes and hours in the time span, then taking the remaining seconds. The modulooperator is useful here, as is the fact that an hour is 3600 seconds, and a minute is 60 seconds:

假设刻度以秒为单位(如果不是,您可以先将它们转换为秒),您可以通过查找时间跨度中的整分钟和小时数,然后获取剩余的秒数来获得所需的格式。该运营商在这里很有用,因为是事实,一个小时3600秒,一分钟是60秒:

function displayTime(ticksInSecs) {
    var ticks = ticksInSecs;
    var hh = Math.floor(ticks / 3600);
    var mm = Math.floor((ticks % 3600) / 60);
    var ss = ticks % 60;

    alert( pad(hh, 2) + ":" + pad(mm, 2) + ":" + pad(ss, 2) );
}

function pad(n, width) {
    var n = n + '';
    return n.length >= width ? n : new Array(width - n.length + 1).join('0') + n;
}

See this answerfor how to pad a number with leading zeros in JS (the method above is derived from this answer).

请参阅此答案以了解如何在 JS 中用前导零填充数字(上述方法源自此答案)。

回答by Gone Coding

This is an old question, but I was not happy with any of the answers.

这是一个老问题,但我对任何答案都不满意。

There are 3 issues here:

这里有3个问题:

  • Converting the tick size to seconds
  • Extracting the hours, minutes and seconds from a duration in seconds
  • Formatting the numbers to have 2 digits
  • 将刻度大小转换为秒
  • 从以秒为单位的持续时间中提取小时、分钟和秒
  • 将数字格式化为 2 位数

Part 1 - Ticks

第 1 部分 - 蜱虫

Tick sizes for media come in many durations, so some assumptions need to be made in order to answer the question.

媒体的刻度大小有很多持续时间,因此需要做出一些假设才能回答这个问题。

As the desired answer is in seconds, minutes and hours, the first step is to convert the "tick" value to seconds.

由于所需的答案以秒、分钟和小时为单位,因此第一步是将“刻度”值转换为秒。

 seconds = ticks / tickspersecond

For example, if the ticks were in milliseconds, then the conversion would be

例如,如果滴答以毫秒为单位,那么转换将是

 seconds = ticks / 1000

Part 2 - extracting hour, minute & second

第 2 部分 - 提取小时、分钟和秒

  • hour = seconds / secondsperhour=> hour = seconds / 3600
  • minute = seconds / secondsperminute modulus minutesperhour=> minute = (seconds / 60) % 60)
  • second = seconds modulus secondsperminute=> second = seconds % 60
  • hour = seconds / secondsperhour=> hour = seconds / 3600
  • minute = seconds / secondsperminute modulus minutesperhour=> minute = (seconds / 60) % 60)
  • second = seconds modulus secondsperminute=> second = seconds % 60

Part 3 - formatting

第 3 部分 - 格式化

  • There are a number of ways to format leading zeros on numbers, this one will use a simple function specific to this example (max of 2 digits).
  • 有多种方法可以格式化数字的前导零,本例将使用特定于该示例的简单函数(最多 2 位数字)。

e.g.

例如

function pad2(number){
   number = '0' + number;
   return number.substr(number.length - 2);
}

Note: Due to floating point errors, you need to apply Math.floor to both the hour and minute values. You could put Math.floorinto the pad2function if you want to shorten the main code.

注意:由于浮点错误,您需要将 Math.floor 应用于小时和分钟值。你可以把Math.floorpad2,如果你想缩短主码功能。

The final answer becomes:

最后的答案变成了:

// Assume milliseconds for now
var seconds = ticks / 1000; 
var hour = Math.floor(seconds / 3600);
var minute = Math.floor((seconds / 60) % 60);
var second = seconds % 60;

var result = pad2(hour) + ':' + pad2(minute) + ':' + pad2(second)

Note: For the sake of the demo I added % 24(modulus hoursperday) to the hours display as the hours since 1971 is a "big" number - so will not format to 2 decimal places :)

注意:为了演示,我将% 24( modulus hoursperday)添加到小时显示中,因为自 1971 年以来的小时数是一个“大”数字 - 所以不会格式化为 2 个小数位:)

JSfiddle Demo:https://jsfiddle.net/TrueBlueAussie/x2s77gzu/

JSfiddle 演示:https ://jsfiddle.net/TrueBlueAussie/x2s77gzu/

or if you want to put Math.floorin the pad2https://jsfiddle.net/TrueBlueAussie/x2s77gzu/1/

或者,如果你想放Math.floorpad2https://jsfiddle.net/TrueBlueAussie/x2s77gzu/1/

回答by Mike Fuchs

Assuming the ticks are in javascript format (milliseconds since midnight Jan 1, 1970):

假设刻度是 javascript 格式(自 1970 年 1 月 1 日午夜以来的毫秒数):

var date = ticks.toISOString(); // gives e.g. 2015-10-06T16:34:23.062Z
var time = date.substring(11,19);  // 16:34:23

http://www.w3schools.com/jsref/jsref_toisostring.asp

http://www.w3schools.com/jsref/jsref_toisostring.asp

EDIT: enter image description here

编辑: 在此处输入图片说明

回答by Kees C. Bakker

You'll need the following code:

您将需要以下代码:

//define ticks
var ticks = {...};

//get seconds from ticks
var ts = ticks / 1000;

//conversion based on seconds
var hh = Math.floor( ts / 3600);
var mm = Math.floor( (ts % 3600) / 60);
var ss = (ts % 3600) % 60;

//prepend '0' when needed
hh = hh < 10 ? '0' + hh : hh;
mm = mm < 10 ? '0' + mm : mm;
ss = ss < 10 ? '0' + ss : ss;

//use it
var str = hh + ":" + mm + ":" + ss;
console.log(str);