2

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?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Kaherdin
  • 2,055
  • 3
  • 23
  • 34

1 Answers1

5

momentjs is in legacy state and explained in detail in the project status page.

Their recommended alternative 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.

The following formats it like you have asked: 14h

DateTime.now().toFormat("HH'h'")
Ryan
  • 948
  • 6
  • 20
  • 1
    Thanks for the Luxon recommendation. In my case, this works : DateTime.fromISO(schedule.scheduleEndTime).toFormat("HH'h'mm") – Kaherdin Jun 22 '21 at 01:45
  • 1
    Awesome! If this answer helped solve your problem, please mark it as correct :) – Ryan Jun 22 '21 at 03:02