Categories
JavaScript Answers

How to convert 12 hour (AM/PM) string to 24 hour string using moment.js and JavaScript?

Spread the love

Sometimes, we want to convert 12 hour (AM/PM) string to 24 hour string using moment.js and JavaScript.

In this article, we’ll look at how to convert 12 hour (AM/PM) string to 24 hour string using moment.js and JavaScript.

How to convert 12 hour (AM/PM) string to 24 hour string using moment.js and JavaScript?

To convert 12 hour (AM/PM) string to 24 hour string using moment.js and JavaScript, we use the format method.

For instance, we write

const dt = moment("12:15 AM", ["h:mm A"]).format("HH:mm");

to create a moment object from the time string with moment.

Then we format it into a 24 hour time string by calling format with 'HH:mm'.

Conclusion

To convert 12 hour (AM/PM) string to 24 hour string using moment.js and JavaScript, we 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 *