Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

choose which token to trim/show when value is 0 #22

Closed
farfromrefug opened this issue Dec 23, 2014 · 10 comments
Closed

choose which token to trim/show when value is 0 #22

farfromrefug opened this issue Dec 23, 2014 · 10 comments
Milestone

Comments

@farfromrefug
Copy link

I am trying to format ms into a "progress" time.
so what i would like is for

moment.duration(120000).format(_theFormat); // "2:00"
moment.duration(31000).format(_theFormat); // "0:31"
moment.duration(3620000).format(_theFormat); // "1:00:20"

Is there already a way to do that?
(i dont think so)

On way would be to use format h:m:ss but with an "option" to trim h but not mm.
I hope it make sense

@jsmreese
Copy link
Owner

jsmreese commented Jan 5, 2015

That's a good use case. I can't think of a way to do it directly in this version, but a new minToken option used by the trim operation would work.

@danatcofo
Copy link

I would love this as well! right now I'm having to do a case statement to ensure that the seconds token is always displayed

@jsmreese jsmreese added this to the v2.0.0 milestone Feb 19, 2015
@jsmreese
Copy link
Owner

Would you prefer to set an option:

moment.duration(10, "seconds").format("h:mm:ss", { trimUntil: "minutes" });
// "0:10"

Or would you prefer an addition to the format grammar:

moment.duration(10, "seconds").format("h:*mm:ss");
// "0:10"

I'm leaning towards an addition to the format grammar. It seems like it would be easier to use that way.

@jsmreese
Copy link
Owner

moment.duration(15, "seconds").format("h:*mm:ss");
// "0:15"

moment.duration(15, "seconds").format("h:*mm:ss", { forceLength: true });
// "00:15"

@farfromrefug
Copy link
Author

I think i prefer the second version with the *.THanks

@danatcofo
Copy link

Either the trimUntil or the * would work but the token is much more consumable in my opinion. You can store it on a single attribute for automated formatting and keep it simple where the settings object forces any consumer or code to be more complex.

@danatcofo
Copy link

Another thing maybe unrelated but if you have mm I would expect the output to force length to 2 as opposed to a single m which would trim the leading 0s

@jsmreese
Copy link
Owner

I've implemented the * functionality on my dev branch. Please give it a try if you get a chance!

Also, @danatcofo:

When the leading token has a length of one (as in h:*mm:ss) and it is trimmed off, you need to use forceLength: true to get a leading zero on the mm output. But when the leading token has a length greater than one (as in hh:*mm:ss) and it is trimmed off, the leading zero on the mm output is preserved. Rationale for that comes from user testing back when I first created the plugin:

If the template is h:mm:ss and the moment value is 5 minutes, the user-friendly output is 5:00, not 05:00. Shouldn't pad the minutes token even though it has length of two.
If the template is h:mm:ss and the minutes output should always include the leading zero even when the hour is trimmed then set { forceLength: true } to output 05:00.
If the template is hh:mm:ss, the user clearly wanted everything padded so we should output 05:00.
If the user wanted the full padded output, they can use template hh:mm:ss and set { trim: false } to get 00:05:00.

@alex7kom
Copy link

alex7kom commented Oct 1, 2015

Works for me, thank you!

@CalebKester
Copy link

Thanks for adding this feature in to you dev branch! Works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants