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
duration formatting #1048
Comments
+1 |
|
Isn't it better to do
instead of
|
@robinvdvleuten - The problem with using the |
I noticed it indeed. I'm now using the calculated milliseconds to format it, but it's still a hacky workaround. |
Are you thinking there will just be one "level" of remaindering possible--e.g. no way to say "hours after accounting for months"? I think that's a perfectly reasonable limitation, but just to spitball a sec, I could imagine providing an "anchor" argument, like so: moment.duration(x).format("hh:mm:ss", "M"); Which tells us that the hours should be modulo the month. The anchor is optional and would default to the largest unit plus one size up (in this case, days). Similarly, it could use the string itself to determine that the minutes are modulo the hours and seconds modulo minutes. Whereas if you did this: moment.duration(x).format("hh:ss"); ...the seconds would be modulo the hours. That's probably all not a great idea--it's not ironed out (where to weeks go in this hierarchy?) and it's probably unnecessary (people are unlikely to want holes like that). But fun to think about. |
On a more serious note, how are you going to deal with month vs minutes with this lowercase/uppercase notation? |
I was envisioning that days would be the maximum unit size, so months wouldn't enter the picture. Basically, this is the inverse of how we parse durations coming from asp.net time spans. |
Makes sense. |
I have been reading a SCORM 2004 RTE (p77) document, and i found that for time interval representation is as follows, i think this came from a standar, in theory i can pass a value like: P34H i hope it could be posible to format the ouput with this in mind i hope this info is helpfull timeinterval (second, 10,2):The timeinterval (second, 10, 2)denotes that the value for Implementers should be aware that the format and binding is for the communication of |
Duration formatting would be an excellent addition. There's no need for two distinct "hour" tokens. Assume a duration of 32 hours. You'd never want to extract just 8 hours without also extracting 1 day. I think @icambron has a good suggestion. Parse the format string into tokens and express the duration in terms of the largest unit. Parse the tokens one at a time in order (e.g., "DD:hh:ss" meaning number of complete days, number of complete hours after accounting for days, number of complete seconds after accounting for days + hours). |
I've posted a moment.duration.format plugin: I think it addresses most of the ideas / use-cases in this thread. |
@jsmreese Have you consider filing a pull request that contains your plugin as a core part of momentjs? |
@hotzenklotz Yes I have considered filing a pull request. I haven't done so because of all the reasons @icambron laid out in #1538. My plugin:
I would love for my plugin to become part of Moment.js core... but I'm not going to waste their time with a pull request before those issues are addressed. |
We would also want to make sure that the code can be internationalized. Fortunately, most of the strings we would need are in the Unicode CLDR, so very little translation work should be necessary. The CLDR also has locale-specific recommendations on how to format certain kinds of intervals, which could potentially be more useful than arbitrary duration formats. Not sure how that fits in here, but it may be nice to automagically be able to display locale-specific intervals between two specific times. The CLDR also has information about how to display specific time intervals (rather than durations) which could be useful at some point... |
Just published a new version of Moment Duration Format that removes the previous dependency on Lo-Dash or Underscore. |
@jsmreese Suits our needs perfectly. Thanks! |
+1 |
5 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
4 years and still no reaction from owners... That's sad :( |
Hi folks, This is something we're still working on. Moment is a volunteer community! We need to make sure a feature like this has these properties:
It's possible this will merge at some point. If you think the docs could better point to the duration formatting plugin (or other plugins!), please send a PR our way here: https://github.com/moment/momentjs.com/ |
Well, since there is already duration-format plugin and duration inside moment library, maybe the best solution is to take out duration from moment.js into separate plugin and then implement all the "heavy" stuff inside that plugin?
|
One large challenge is that duration formatting will be very difficult to implement across the full range of locales that moment.js supports. I'd hate to see those locales lose the limited duration support that they currently have, or for Moment to add a feature that only works correctly in some locales. Keeping duration formatting in a plugin seems like a good middle-ground that ensures that the "core" of moment.js is stable, and works for everyone, while giving single-locale users the option to use plugins that perform functions specific to their locale. |
The duration formatting plugin already exists and is referenced inside the documentation. Thats more than enough. |
@OogieBoogieInJSON Well, the docs aren't that helpful, i basically had to check this issue before even getting to the plugins in the docs, there's little exposure to those features unless you actually try and read the whole docs, which admittedly nobody does. |
@luchillo17 I do read all the docs before working with anything. Probably, thats just me. |
Haha, yeah, it's great that you do, everyone should, but most of us as devs have schedules to meet, so understanding every nut & bolt of all libs we use isn't practical. |
@luchillo17 It's not the documentation's fault you do Management Oriented Programming. Cheers! |
For those following the saga of formatting moment durations, I've published version 2.0.0 of my moment-duration-format plugin. The new version resolves/incorporates almost all the issues and feedback from the past four years version 1 has been in the wild -- including localization and pluralizing support as well as some useful formatting options. Check it out here: https://github.com/jsmreese/moment-duration-format/releases |
The real MVP -> @jsmreese |
Hah. Thanks for the kind words, @OogieBoogieInJSON. I can't help but note that my plugin stands on top of -- and wouldn't exist without -- the huge efforts of moment's creators and maintainers, not to mention the many, many contributors. My taking the time now to revisit something I created four years ago (wow, has it really been that long!) quite frankly pales in comparison to their continuous shepherding of this project! |
Aaaaaand version 2.1.0 is published. Updated version fixes a few issues from version 2.0, and introduces The new function takes an array of durations, and returns an array of formatted strings, and is useful whenever you have a group of durations that must be formatted together in a consistent manner. moment.duration.format([
moment.duration(1, "second"),
moment.duration(1, "minute"),
moment.duration(1, "hour")
], "d [days] hh:mm:ss");
// ["0:00:01", "0:01:00", "1:00:00"]
moment.duration.format([
moment.duration(1, "minute"),
moment.duration(1, "day")
], "w [weeks], d [days], h [hours], m [minutes], s [seconds]", { trim: "all" });
// ["0 days, 1 minute", "1 day, 0 minutes"] |
@jsmreese 2.0 did lead to some errors earlier so I had to lock it to 1.3.0 for safety, nevertheless thank you for keeping the feature/project alive. |
@prusswan please do give version 2.1.0 a try. I'd like to know if you still see those errors! |
I've published version 2.2.0 of moment-duration-format, which now includes a fallback number format function because https://github.com/jsmreese/moment-duration-format/releases I've tested the new version using BrowserStack on a range of Android devices with OS versions from 2.2 to 7, and on a range of iOS devices with OS versions from 4.3 to 11. Also tested on Chrome, Firefox, IE 8-11, and Edge browsers. @prusswan and others who had to lock the version to Thank you to all who have logged issues against version 2 of this plugin! |
Having implemented version 2 of the moment-duration-format plugin, there are some obvious improvements for a version 3. I've listed my ideas below and added them as issues on the repository. If you have ideas or comments about what you'd like to see, please let me know! Hopefully the next version will be published in something like 4 months... not the 4-year wait for version 2.
|
@jsmreese Tremendous work, but totally don't feel pressured to bump the major version twice within a short period of time. If it is still in use after 4 years without an update, then it is probably mostly good enough. I feel this current issue can be closed since there is already a resolution (use the plugins). A new issue can be started to decide if this functionality should become part of moment itself. |
@jsmreese Amazing. Thank you so much and just in time! Huge deal for me and thank you for all that you do. |
While the attendances, duties and events features are otherwise separate, upstream changes to attendances and duties must be propagated to the events list and therefore agenda feed. Events are: - A duty is created (CreateSuccess). - A duty is destroyed (DestroySuccess). - An attendance is updated (UpdateSuccess). Changes: - Modules - Add DurationModule - Encapsulate DurationComponent and DurationPipe. These are used together in duties and attendances - Pipes - DurationPipe - Refactor. The original code threw an error when called as I used deprecated syntax. See linked GitHub issue for more information. - Update calling references. - Builders - CalendarEventbuilder - Add ID getters for attendance and duty records. EventEffects use these getters. - Update internal refernces to use these. - Effects/Reducers - Add EventEffects - Forward propagate appropriate attendance and duty changes to the events refucer. - Add tests for all affected code. See: moment/moment#1048 (comment) Closes: d4h/decisions-project#4472
See https://momentjs.com/docs/#/-project-status/ Thanks for all the discussion here. |
We've touched on this before in #463 and #879 but never really talked about it directly in its own issue.
We need a method for formatting durations similar to how we format dates. It should be as simple as:
Note that the formatting tokens would have to carry slightly different meaning, since we're representing an elapsed duration of time, rather than a time-of-day. I suggest the following:
hh
would mean "hours remainder after accounting for days"h
would be the single digit form ofhh
HH
would mean "total whole hours"H
would be the single digit form ofHH
HHH
would mean "total hours including decimals" - although that can currently be done withduration.asHours()
, so this might not be necessary.Similar formatting would apply for other units. In this context, the highest unit would be a "day", which would be a standard day consisting of 24 standard hours. It wouldn't make sense to measure years or months with this due to calendaring issues.
Note, this came up recently (again) on StackOverflow in this question. The user was looking for something like
duration.format("H:mm:ss")
. This is the workaround:This works, but it feels hacky. This should be built in.
The text was updated successfully, but these errors were encountered: