Dayjs: Any workaround for format duration in h m
Asked Answered
P

1

10

I am using dayjs library's duration object for duration format.

dayjs.duration(1500, 'minutes').format('H[h] m[m]') // returns 1h 0m

Seems the library converts the 24hrs to 1 day and puts the remaining 1 hour in hours. Actually, I want the output as 25h 0m.

Is there any way we can achieve this with days.js or any minimal custom logic along with the use of duration object?? Or any other library provides this functionality itself??

Pouched answered 6/2, 2022 at 6:22 Comment(1)
Did you manage to display 25h00 instead of 1 day 1 hour ?Blossomblot
S
5

Dayjs's documentation about duration formatting suggests you have to give it the formatted including the day part if you want to get the duration in days.

So something like this is expected on the format string:

dayjs.duration(1500, 'minutes').format('D[d] H[h] m[m]')

==> "1d 1h 0m"

Similar would be expected for months and years.

Alternatively you could use the humanise helper, but it is less precise:

dayjs.duration(1500, 'minutes').humanize()

==> "a day"
Saipan answered 6/2, 2022 at 6:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.