简体   繁体   中英

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

I am trying to convert seconds in hh:mm:ss format . 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. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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