How to get 'a.m.' format in moment.js?

Member

by lizzie , in category: Javascript , a year ago

How to get 'a.m.' format in moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , a year ago

@lizzie 

To get the 'a.m.' format using moment.js, you can use the 'A' token in the format() function. Here's an example:

1
2
3
4
var now = moment(); // Current date and time
var amFormat = now.format('h:mm a'); // 1:30 p.m.

console.log(amFormat);


In the format() function, 'h' represents the hour in a 12-hour format (1-12), 'mm' represents the minutes, and 'a' represents the lowercase 'am' or 'pm' specifier.


Remember to import the moment.js library before using it:

1
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>


By using the format 'h:mm a' you will get a result like '1:30 p.m.' which includes the 'a.m.' or 'p.m.' marker.