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

Duration formatting #2162

Closed
dennismonsewicz opened this issue Jan 15, 2015 · 7 comments
Closed

Duration formatting #2162

dennismonsewicz opened this issue Jan 15, 2015 · 7 comments

Comments

@dennismonsewicz
Copy link

When using the Moment.duration({ days: 67 }).toIsoString() portion of this library, I am noticing that the ISOString that is formatted is P2M7D, but, what I need is P67D which is a more accurate representation of what I need.

Is there anyway, using Moment.js, to achieve this? Maybe through some kind of passed in option?

@Jervelund
Copy link

As toISOString uses the internal values of _data for the object, the format outputted by toISOString is determined on object creation. Very hacky example for modifying days:
var v = moment.duration(1,'days');v._data['days'] = 300;v.toISOString(); outputs P300D

Could you use moment.duration(200,'days').asDays(); and prepend P and append D to the string?

@dennismonsewicz
Copy link
Author

@Jervelund I wound up creating this mix-in https://gist.github.com/dennismonsewicz/4f6b4b94f14149b590ca for the project I was working on

@NobodysNightmare
Copy link
Contributor

Disclaimer first: I did not buy the ISO specification, so my knowledge about ISO8601 is from wikipedia and other implementations.

I assume that moment does not convert correctly according to ISO rules. For example the following two inputs result in the same output:

> moment.duration(24, 'hours').toString()
"P1D"
> moment.duration(1, 'days').toString()
"P1D"

However, I'd expect different outputs (expectation):

> moment.duration(24, 'hours').toString()
"PT24H"
> moment.duration(1, 'days').toString()
"P1D"

According to the german wikipedia both ISO representations differ at least in case of a clock change (DST), where P1D means: tommorrow, same time and PT24H means: in 24 hours, that might be one hour ealier or later on the clock.

The problem gets even more obvious when you take examples like this one:

> moment.duration(720, 'hours').toString()
"P1M"

720 hours is the duration of 30 days, which is the duration of a month. Add different libraries creating (moment) and parsing (third party) that ISO date and you come up with quite a lot of fun ;-)

For my case I will also have to verify what the parsing library does and if that is correct. But I am quite sure, that it is invalid (from an ISO 8601 standpoint) to simply convert 720 hours to one month.

I think the bubbling (see here) should stop at the level of hours.

@NobodysNightmare
Copy link
Contributor

One addition: I assume somebody working on moment already knew that this would be sensible:

moment.duration(24, 'hours')
Object { _milliseconds: 86400000, _days: 0, _months: 0, _data: Object, _locale: Object }
moment.duration(1, 'days')
Object { _milliseconds: 0, _days: 1, _months: 0, _data: Object, _locale: Object }

The difference between 24 hours and 1 day is present in the object, it just vanishes when it comes to ISO formatting...

@bendavis78
Copy link

Shouldn't duration formatting work similar to date formatting?

moment.duration(330).format('h:mm:ss')

There is already a library that does this (jsmreese/moment-duration-format), but it would make sense to include this in the base library.

@NobodysNightmare
Copy link
Contributor

So this was a long time ago and I don't really remember the details, but didn't PR #2357 fix this? In that case this issue could probably be closed :)

@marwahaha
Copy link
Member

Closing, as this is partly fixed and partly a duplicate of #1048

Let me know if anyone has further thoughts.

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

No branches or pull requests

6 participants