Need Help: Parsing Time Strings Like JIRA's Format

Hey everyone! I’m working on a project where I need to handle time strings that look like this: 3w 2d 5h 30m. It’s kinda like how JIRA shows time durations. I want to turn these strings into milliseconds so I can do math with them, like adding or subtracting from timestamps.

Does anyone know of a good library that can handle this kind of parsing? I’ve been searching but haven’t found anything that fits the bill yet. It needs to understand years, months, weeks, days, hours, minutes, and seconds.

If there’s no ready-made solution, I might have to write my own parser. Any tips on how to approach this would be super helpful too! Thanks in advance for any suggestions!

I’ve faced similar challenges in my projects. Instead of searching endlessly for a library, I implemented my own parser. I began by splitting the input string into components, then mapping each time unit to its corresponding millisecond value. I used regular expressions to extract the numerical value and its unit, multiplied the number by the conversion factor, and finally summed the values for a total. This approach gave me flexibility and room for customization. It is important to account for months and years carefully since their durations can vary.

Having dealt with similar time parsing issues, I can offer some insights. While a custom parser is viable, it’s worth exploring existing libraries first. Moment.js with its duration parsing capabilities could be a good fit, though it’s heavy for just this task. A lighter alternative is the ‘parse-duration’ npm package, which handles formats similar to what you described. If you decide to build your own, consider using a regex-based approach to extract numeric values and units. Map these to milliseconds, being mindful of edge cases like varying month lengths. Remember to handle potential input errors gracefully. Whichever route you choose, thorough testing with various input formats is crucial to ensure reliability.

hey mate, i’ve been there! honestly, the easiest way is probably to write ur own parser. split the string, use a regex to grab numbers and units, then multiply by milliseconds for each unit. just watch out for months - they’re tricky. good luck with ur project!