Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using moment.js to convert time (HH:mm:ss) to (HH:mm)

I was wondering if I could use moment.js to convert an sql TIME (HH:mm:ss) to just HH:mm. Or maybe there is a simple JavaScript/jQuery solution to this?

like image 572
Mistergrave Avatar asked Apr 30 '15 19:04

Mistergrave


People also ask

What will Moment () return?

moment() returns a date and format() converts the date string tokens and replaces them with specified format values, which are readable.

How do you format time in a moment?

moment('24/12/2019 09:15:00', "DD MM YYYY hh:mm:ss"); You can use strict mode, which will identify the parsing error and set the Moment object as invalid: moment('24/12/2019 09:15:00', "DD MM YYYY hh:mm:ss", true); The parsing tokens are similar to the formatting tokens used in moment#format .

What does format () do in moment?

format(); moment(). format(String); This is the most robust display option. It takes a string of tokens and replaces them with their corresponding values.


1 Answers

Yes, you can:

moment('12:59:01', 'HH:mm:ss').format('HH:mm')

Or, if the format is consistent, you could just chop the last 3 chars:

'12:59:01'.slice(0, -3)
like image 180
Andrew Lavers Avatar answered Oct 05 '22 18:10

Andrew Lavers