Skip to content
Advertisement

How to construct a timedelta object from a simple string

I’m writing a function that needs to parse string to a timedelta. The user must enter something like "32m" or "2h32m", or even "4:13" or "5hr34m56s"… Is there a library or something that has this sort of thing already implemented?

Advertisement

Answer

For the first format (5hr34m56s), you should parse using regular expressions

Here is re-based solution:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement