简体   繁体   English

使用moment.js将秒转换为hh:mm:ss格式

[英]Convert seconds into hh:mm:ss format using moment.js

I am trying to convert seconds in hh:mm:ss format . 我试图以hh:mm:ss格式转换秒数。 I have using below code to convert seconds into hours,min. 我使用下面的代码将秒转换为小时,分钟。 but I don't want to do all this. 但我不想做这一切。 Is it possible in moment.js to convert seconds into HH:MM:SS format. 是否有可能在moment.js中将秒转换为HH:MM:SS格式。 rather than doing it manually. 而不是手动完成。

var x = 43000
var durationValue = moment.duration(x, 'milliseconds');
var hours = Math.floor(durationValue.asHours());
var mins = Math.floor(durationValue.asMinutes()) - hours * 60;
console.log("hours:" + hours + " mins:" + mins);
moment("1900-01-01 00:00:00").add(43000/1000, 'seconds').format("HH:mm:ss")

try this? 尝试这个?

moment's duration doesn't have millisec in their definition, you should divide it by 1000. 时刻的持续时间在定义中没有毫秒数,你应该除以1000。

http://momentjs.com/docs/#/manipulating/add/ http://momentjs.com/docs/#/manipulating/add/

http://momentjs.com/docs/#/durations/ http://momentjs.com/docs/#/durations/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM