Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 951
DarrenW Avatar asked Jan 06 '23 12:01

DarrenW


2 Answers

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

like image 113
baao Avatar answered Jan 08 '23 02:01

baao


moment("13:15:00", "h:mm:ss").format("hh:mm A"); it will give 12 hrs format

like image 26
vinod inti Avatar answered Jan 08 '23 02:01

vinod inti