Format hour to 24h

Viewed 365

I've got an hour in a string format like this : 14:00:00.000 I want to convert it in a nicer format : 14h

I try to use react-moment for this, with

<Moment format="hh:mm:ss.a">"14:00:00.000"</Moment>

But it doesn't work. Any good practice?

1 Answers

momentjs is no longer recommended for use and is explained in detail in the project status page.

Their recommended alternate is Luxon.

To format it like you have specified, simply use the toFormat() method. Included in that link is a list of tokens you can use to help format it to exactly how you'd like it.

This formats it like you have asked - 14h:

DateTime.now().toFormat("HH'h'")
Related