jsmreese / moment-duration-format

Format function plugin for the Moment Duration object.
MIT License
968 stars 120 forks source link

incorrect formatting of 'd:hh:mm:ss' #108

Closed ness-EE closed 6 years ago

ness-EE commented 6 years ago

Simple one:

moment.duration(3929307, 'milliseconds')
      .format('d:hh:mm:ss')
// actual '1:05:29'
// expected '01:05:29'

moment.duration(3929307, 'milliseconds')
      .format('dd:hh:mm:ss'));

// actual & expected '01:05:29'

It seems that making the day a single digit affects the formatting of the hour

jsmreese commented 6 years ago

@ness-EE this is by design. For your use case, you'll want to set the forceLength option to true. https://github.com/jsmreese/moment-duration-format#forcelength

ness-EE commented 6 years ago

Hi - thanks for the pointer! What's the thinking behind the design pattern? if I put 'hh' I expect it to be double digit, right? Is there something I might be missing?

jsmreese commented 6 years ago

It's based on defaulting to the most user-friendly output.

It's easier to read 5:00 than to read 05:00 or 0:05:00. Show to the user only the information that is necessary, remove all else... the leading 0 padding is usually not required to comprehend the meaning.

https://github.com/jsmreese/moment-duration-format/blob/master/lib/moment-duration-format.js#L1042-L1061