Javascript – Turn a String to a 24 hour time format, using moment.Js

javascriptmomentjs

I am trying to format a String into a 24 hour format using moment JS at the moment I am not getting the expected output, the below explains it in more detail:

How I am coverting

 var testFormat = moment("9:00","HH:mm:ss");

I was hoping this would output:

09:00:00 (24 Hour format)

But for some reason is does not convert just the date, this is outputing the following:

1471507200000

Best Answer

You are using it wrong. You can provide a format as the second argument to moment. So in your case it would be:

var myMoment = moment("9:00","H:mm");

To get the moment. Now you can format it the way you want:

myMoment.format("HH:mm:ss");

DEMO

Related Topic