jsmreese / moment-duration-format

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

Consider supporting ISO 8601's duration format (Periods) as input? #117

Closed yanndinendal closed 6 years ago

yanndinendal commented 6 years ago

I looked at the doc and it looks like it's not possible, so here is a suggestion.

Would you consider supporting the standard Duration format as input (a single string instead of a number and a string)?

jsmreese commented 6 years ago

From the linked article:

For example, "P3Y6M4DT12H30M5S" represents a duration of "three years, six months, four days, twelve hours, thirty minutes, and five seconds".

Are you saying that you want to do this to instantiate a Duration object?

moment.duration("P3Y6M4DT12H30M5S");

Or are you saying that you'd like some method to easily output a duration formatted to that standard?

yanndinendal commented 6 years ago

I would like to use it as input, not output.

The output would come from a REST API (it's a bit more human-readable than a timestamp in seconds), and then the js frontend would parse it via moment-duration-format and format it with any of the existing available formats. :)

jsmreese commented 6 years ago

The moment-duration-format plugin is strictly for creating formatted text output from existing duration objects. It has nothing to do whatsoever with parsing inputs to duration objects.

To update things on the input side, you'll want to file an issue with Moment.js directly.

yanndinendal commented 6 years ago

Oh ok thanks! I didn't understand that the moment.duration(123, "minutes") part of moment.duration(123, "minutes").format("h:mm"); in the examples came from moment itself! :)

yanndinendal commented 6 years ago

https://momentjs.com/docs/#/durations/creating/ (https://github.com/moment/momentjs.com/blob/master/docs/moment/08-durations/01-creating.md)

As of 2.3.0, moment also supports parsing ISO 8601 durations.

moment.duration('P1Y2M3DT4H5M6S');
moment.duration('P1M');

Ok, so it's already supported! :D

@jsmreese: Maybe the README should link to moment's Duration section in its documentation?

https://momentjs.com/docs/#/durations/