我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)

我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。

那么有一个小片段,这是在jQuery或只是原始JavaScript?


当前回答

下面是一个使用Date.prototype.toLocaleTimeString()的例子。我选择GB作为语言,因为美国在最初的小时显示的是24而不是00。此外,我选择Etc/UTC作为时区,因为UTC在tz数据库时区列表中是它的别名。

const formatTime = (seconds) => 新日期(秒* 1000)。toLocaleTimeString(“en”{ 时区:“等/ UTC ', hour12:假的, 小时:“便是”, 分钟:“便是”, 第二:“便是” }); console.log (formatTime (75));/ / 00:01:15 .as-console-wrapper {top: 0;Max-height: 100%重要;}

下面是相同的示例,但是使用了Intl.DateTimeFormat。这个变体允许您实例化一个可重用的格式化器对象,这更具有性能。

const dateFormatter = new Intl。DateTimeFormat(“en”{ 时区:“等/ UTC ', hour12:假的, 小时:“便是”, 分钟:“便是”, 第二:“便是” }); const formatTime = (seconds) => dateFormatter。format(new Date(seconds * 1000)); console.log (formatTime (75));/ / 00:01:15 .as-console-wrapper {top: 0;Max-height: 100%重要;}

其他回答

要获得格式为hh:MM:ss的时间部分,可以使用下面的正则表达式:

(上面有人在同一篇文章中提到了这一点,谢谢。)

var替换= new日期().toTimeString () .replace (/ . * (\ d {2}: \ d {2}: \ d{2})。* / " $ 1 "); console.log(替换)

我推荐使用普通的javascript,使用Date对象。(要获得更简短的解决方案,请使用toTimeString,请参阅第二个代码片段。)

var seconds = 9999;
// multiply by 1000 because Date() requires miliseconds
var date = new Date(seconds * 1000);
var hh = date.getUTCHours();
var mm = date.getUTCMinutes();
var ss = date.getSeconds();
// If you were building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time
// if (hh > 12) {hh = hh % 12;}
// These lines ensure you have two-digits
if (hh < 10) {hh = "0"+hh;}
if (mm < 10) {mm = "0"+mm;}
if (ss < 10) {ss = "0"+ss;}
// This formats your string to HH:MM:SS
var t = hh+":"+mm+":"+ss;
document.write(t);

(当然,创建的Date对象将有一个与之关联的实际日期,但该数据是无关的,因此出于这些目的,您不必担心它。)


编辑(简短的解决方案):

使用toTimeString函数并对空白进行分割:

var seconds = 9999; // Some arbitrary value
var date = new Date(seconds * 1000); // multiply by 1000 because Date() requires miliseconds
var timeStr = date.toTimeString().split(' ')[0];

toTimeString给出'16:54:58 GMT-0800 (PST)',对第一个空格进行分割给出'16:54:58'。

以下是@meiyang的优秀解决方案的一个变体,我最终使用了它:

    function duration(seconds) {
        return [
            format(seconds / 60 / 60),
            format(seconds / 60 % 60),
            format(seconds % 60)
        ].join(':');
    }

    format(n) {
        return (~~n).toString().padStart(2, '0')
    }

PS:同样值得注意的是,上面的一些其他解决方案只适用于< 24h的值

这是我最近为MM:SS写的。它不完全符合问题,但它是一种不同的单行格式。

const time = 60 * 2 + 35; // 2 minutes, 35 seconds
const str = (~~(time / 60) + "").padStart(2, '0') + ":" + (~~((time / 60) % 1 * 60) + "").padStart(2, '0');

str // 02:35

编辑:添加这个是为了多样化,但这里最好的解决方案是下面的https://stackoverflow.com/a/25279399/639679。

下面是一个使用Date.prototype.toLocaleTimeString()的例子。我选择GB作为语言,因为美国在最初的小时显示的是24而不是00。此外,我选择Etc/UTC作为时区,因为UTC在tz数据库时区列表中是它的别名。

const formatTime = (seconds) => 新日期(秒* 1000)。toLocaleTimeString(“en”{ 时区:“等/ UTC ', hour12:假的, 小时:“便是”, 分钟:“便是”, 第二:“便是” }); console.log (formatTime (75));/ / 00:01:15 .as-console-wrapper {top: 0;Max-height: 100%重要;}

下面是相同的示例,但是使用了Intl.DateTimeFormat。这个变体允许您实例化一个可重用的格式化器对象,这更具有性能。

const dateFormatter = new Intl。DateTimeFormat(“en”{ 时区:“等/ UTC ', hour12:假的, 小时:“便是”, 分钟:“便是”, 第二:“便是” }); const formatTime = (seconds) => dateFormatter。format(new Date(seconds * 1000)); console.log (formatTime (75));/ / 00:01:15 .as-console-wrapper {top: 0;Max-height: 100%重要;}