![]() ![]() I've included 3 ways to parse durations in JavaScript in this blog post, they all have their pros and cons: Learn more about Temporal on the Igalia blog. However, durations are part of the Temporal proposal. There is no API to parse durations in JavaScript's Date API. Like any date or time problem there can be gotchas, so "keep in mind that 'PT36H' is not the same as 'P1DT12H' when switching from or to Daylight saving time." Does JavaScript have native support for durations? ![]() You can also ignore "carry over points" like 24 hours or 60 seconds so PT36H and PT1D12H are both valid. You can use decimals in the last and smallest value like in the 0.5Y or 3.025S examples above. PT1M3.025S (1 minute and 3025 milliseconds).P3Y6M4DT12H30M5S (3 years, 6 months, 4 days, 12 hours, 30 minutes, and 5 seconds).You can leave certain intervals off if they don't apply, but you must include the T before any time intervals ( PT). T = delineator between dates and times, necessary to disambiguate between months and minutes.This format displays a duration in the following format: PnYnMnDTnHnMnS where n is the number for the corresponding interval: Durations represent the amount of time between two dates or times. ISO 8601 is a set of standardized date and time formats in an attempt to tame every programmer's favorite challenge. Much like parsing phone numbers, while you can write a regular expression to parse a duration, I don't recommend it. If you've encountered a duration string that looks like P1DT12H36M and been confused about what to do with it, then you're in the right place. ![]()
0 Comments
Leave a Reply. |