Mistergrave
Mistergrave

Reputation: 179

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?

Upvotes: 7

Views: 10400

Answers (2)

Soban Arshad
Soban Arshad

Reputation: 1411

timeFormatting = (time) => {
  return moment(time, 'HH:mm:ss').format('HH:mm')
}

Upvotes: 1

Andrew Lavers
Andrew Lavers

Reputation: 8141

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)

Upvotes: 15

Related Questions