Categories
JavaScript Answers

How to get am pm from the date time string using moment.js and JavaScript?

Spread the love

Sometimes, we want to get am pm from the date time string using moment.js and JavaScript.

In this article, we’ll look at how to get am pm from the date time string using moment.js and JavaScript.

How to get am pm from the date time string using moment.js and JavaScript?

To get am pm from the date time string using moment.js and JavaScript, we use the 'A' format code.

For instance, we write

console.log(
  moment("Mon 03-Jul-2022, 11:00 AM", "ddd DD-MMM-YYYY, hh:mm A").format(
    "hh:mm A"
  )
);

to call format on a moment object with "hh:mm A" to return a time string with the hours and minutes along with am or pm at the end with 'A'.

Conclusion

To get am pm from the date time string using moment.js and JavaScript, we use the 'A' format code.

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 *