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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.