Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Military time to am/pm conversion using moment.js

Tags:

momentjs

I have a military time that I am trying to convert to a 12 hr am/pm format as shown below

console.log(moment("13:00", 'HH:mm').format('HH:mm a'));
console.log(moment("15:00", 'HH:mm').format('HH:mm a'));

But I am getting

13:00 pm 
15:00 pm

as the output, when it should be 01:00 pm and 03:00 pm. Am I missing something on the code ?

like image 340
user6741031 Avatar asked Dec 12 '16 16:12

user6741031


1 Answers

moment docs are pretty clear on formatting - make sure to check them. "H" is 24 hour time. You want "h" instead (or hh for 0 prefixed).

console.log(moment("13:00", 'HH:mm').format('hh:mm a'));
like image 79
jamey graham Avatar answered Dec 25 '22 20:12

jamey graham