I have a strange format case with date-fns and formatting duration. If somebody have an idea what I'm doing wrong:
function formatDuration(duration) {
return duration >= 60000 * 60
? format(new Date(duration), "hh 'h', mm 'min', ss 'sec'")
: duration >= 60000
? format(new Date(duration), "mm 'min', ss 'sec'")
: format(new Date(duration), "ss 'sec'");
}
What I expect:
formatDuration((59 * 60 + 59) * 1000); // '59 min, 59 sec'
formatDuration((60 * 60) * 1000); // '01 h 00 min, 00 sec'
What I get:
formatDuration((59 * 60 + 59) * 1000); // '59 min, 59 sec'
formatDuration((60 * 60) * 1000); // '02 h 00 min, 00 sec'
In the second case, there is an extra hour appearing from nowhere. I don't understand where the problem is. Any idea? Thanks!
Some of you were true that using Date was not a good idea. Manipulating number was more successfull. Here is my solution to my problem:
Maybe there is a more simple solution than this one but its working well and it's ok for all my cases.
Tested with this, all green: