Categories
JavaScript Answers

How to format JavaScript moment.js date in 24h format?

Spread the love

Sometimes, we want to format JavaScript moment.js date in 24h format.

In this article, we’ll look at how to format JavaScript moment.js date in 24h format.

How to format JavaScript moment.js date in 24h format?

To format JavaScript moment.js date in 24h format, we use the "HH:mm:ss" format string.

For instance, we write

const t = moment("01:15:00 PM", "h:mm:ss A").format("HH:mm:ss");

to call moment to parse the "01:15:00 PM" time string with "h:mm:ss A" into a moment object.

Then we call format with "HH:mm:ss" to return a string with 24 hour hour format instead of 12 hour format with AM or PM at the end.

Conclusion

To format JavaScript moment.js date in 24h format, we use the "HH:mm:ss" format string.

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 *