jsmreese / moment-duration-format

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

[SOLVED] moment.duration.format to show 12 sec in the format 00:12 (mm:ss) #68

Closed fntgnn closed 6 years ago

fntgnn commented 7 years ago

Hi. How can I use the format to show for example 12 seconds in the format 00:12 and not only 12? I used format('mm:ss', { forceLength: true } but it does not work.

Thank you. Giovanni.

jsmreese commented 7 years ago

In your case you'd want this:

moment.duration(12, "seconds").format('mm:ss', { trim: false });
// "00:12"

forceLength acts on individual tokens that aren't trimmed, when the leading token in the format string has a length of 1. Sounds more complicated that it is...

On Thu, Mar 16, 2017 at 11:03 AM, Giovanni Fontana <notifications@github.com

wrote:

Hi. How can I use the format to show for example 12 seconds in the format 00:12 and not only 12? I used format('mm:ss', { forceLength: true } but it does not work.

Thank you. Giovanni.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jsmreese/moment-duration-format/issues/68, or mute the thread https://github.com/notifications/unsubscribe-auth/ABN2iAnlY9dobIGwCkxJNm45EHlYRmyTks5rmU8mgaJpZM4Mfbj6 .

-- John Madhavan-Reese Support Team Lead You.i TV john.madhavan-reese@youi.tv

fntgnn commented 7 years ago

Thank you very much!

mikeevstropov commented 6 years ago

No. Is not working as expected

import moment from 'moment'
import momentDurationPlugin from 'moment-duration-format'
momentDurationPlugin(moment)

moment.duration(12, "seconds").format('mm:ss', { trim: false })
// 0:12
moment.duration(12, "seconds").format('mm:ss', { trim: false })
// 0:1
moment.duration(1200, "seconds").format('hh:mm', { trim: false })
// 0:20
"moment": "2.19.4",
"moment-duration-format": "2.1.0",
jsmreese commented 6 years ago

@mikeevstropov what environment are you using?

A quick check in Chrome and I see:

moment.duration(12, "seconds").format('mm:ss', { trim: false })
// "00:12"
moment.duration(12, "seconds").format('mm:ss', { trim: false })
// "00:12"
moment.duration(1200, "seconds").format('hh:mm', { trim: false })
// "00:20"

The output padding in version 2 comes from toLocaleString... are you in an environment with a borked version of toLocaleString?

mikeevstropov commented 6 years ago

@jsmreese Hi! Thank you for fast reply =)

React Native (android)

"react": "16.0.0",
"react-native": "0.51.0",
jsmreese commented 6 years ago

That explains it. As I'm learning, there are lots of environments where you can use moment that don't provide a fully baked toLocaleString.

I'll see what I can do about getting a fallback implementation of toLocaleString into the plugin.