jsmreese / moment-duration-format

Format function plugin for the Moment Duration object.
MIT License
967 stars 121 forks source link

How to display tenths of seconds? #125

Open remisture opened 5 years ago

remisture commented 5 years ago

// NOT OK

moment.duration(389.7, 'seconds').format("m:ss.S");
// Expected 6:29.7
// Actual 6:29.700

moment.duration(389, 'seconds').format("m:ss.S");
// Expected 6:29
// Actual 6:29.0

// OK

moment.duration(389.7, 'seconds').format("m:ss.SS");
// Expected 6:29.70
// Actual 6:29.70

moment.duration(389.7, 'seconds').format("m:ss.SS");
// Expected 6:29.71
// Actual 6:29.71
jbtheard commented 5 years ago

Have you managed to solve this? I'm in the same situation where I would expect S to be in line with moment 1.6.0+ where S, SS, SSS are tenths, hundredths, thousandths of a second respectively.

jsmreese commented 5 years ago

It's not totally what you want, but you can use the precision option:

moment.duration(389.7, 'seconds').format("m:ss", 1);
"6:29.7"

moment.duration(389, 'seconds').format("m:ss", 1);
"6:29.0"

Your use case involves trimming the milliseconds when their value is 0. Which works, except not...

moment.duration(389, 'seconds').format("m:ss.S", { trim: "both" })
"6:29"

moment.duration(389.7, 'seconds').format("m:ss.S", { trim: "both" })
"6:29.700"