Closed stevdompl closed 3 years ago
@stevdompl you could use ISO8601-duration for that. Would be nice if parse-duration
could be used for duration as well.
It would be trivial to implement by just enabling upcase units, I thought. But in ISO8601 there's ambiguity of M
- it can both mean month and minute, depending on preceding P
or T
. That seems to be a bit custom parsing algorithm.
Can you give an example use-case where parsing both ISO 8601 and human durations is worthy?
I wonder if that's conceptually a good fit for this module or better kept apart, in other words what are the real use-cases for ISO8601
Hi, you see my app is build as an middleware that helps me with migration to my new API.
We got a few legacy sources, two new ones that use the ISO8601 format. On the older ones the duration was saved as a text value in human-readable format (that your lib parses), the other two use the C# TimeSpan format.
I've used your lib in conjunction with moment.js and got it working, but the moment.js dependency is very heavy IMHO.
@stevdompl there's this parse-iso-duration for ISO8601, I'd recommend to use it combined with this package, even names match. (1loc, no need for moment.js)
import parseDuration from 'parse-duration'
import parseIsoDuration from 'parse-iso-duration'
new Date(str.includes('P')?parseIsoDuration(str):parseDuration(str))
If there are other arguments for including ISO into parse-duration, feel free to add, but for now the workaround seems proper and even simpler solution.
Hi,
is it possible to add support for ISO 8601 formatted duration strings? I have to add moment.js library as a dependency (besides yours) just for parsing this format.
More details: https://en.wikipedia.org/wiki/ISO_8601#Durations