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?
moment() returns a date and format() converts the date string tokens and replaces them with specified format values, which are readable.
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 .
format(); moment(). format(String); This is the most robust display option. It takes a string of tokens and replaces them with their corresponding values.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With