Categories
JavaScript Answers

How to convert seconds to HH:mm:ss in moment.js and JavaScript?

Spread the love

Sometimes, we want to convert seconds to HH:mm:ss in moment.js and JavaScript.

In this article, we’ll look at how to convert seconds to HH:mm:ss in moment.js and JavaScript.

How to convert seconds to HH:mm:ss in moment.js and JavaScript?

To convert seconds to HH:mm:ss in moment.js and JavaScript, we can use the format method.

For instance, we write

const secs = 456;

const formatted = moment.utc(secs * 1000).format("HH:mm:ss");

console.log(formatted);

to call moment.utc with the sec seconds converted to milliseconds to create a moment object with the milliseconds.

Then we call format with "HH:mm:ss" to convert the milliseconds to a string in HH:mm:ss format.

Conclusion

To convert seconds to HH:mm:ss in moment.js and JavaScript, we can use the format method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *