parseDuration() function is a lower-level API for parsing duration strings. It provides direct access to parsing with explicit language parameters.
Import
Signature
The duration string to parse (e.g.,
"2h 30m", "1 day, 5 hours")The language used for parsing. Obtain a
Language object using getLanguage().The total duration in milliseconds, or
0 if the duration is invalidExamples
Basic usage
With custom language
Flexible input formats
The parser is flexible and accepts various formats:Invalid input
Supported units
The function supports parsing the following time units (in English):- Years (e.g.,
"year","years","yr","y") - Days (e.g.,
"day","days","d") - Hours (e.g.,
"hour","hours","hr","h") - Minutes (e.g.,
"minute","minutes","min","m") - Seconds (e.g.,
"second","seconds","sec","s") - Milliseconds (e.g.,
"millisecond","milliseconds","ms") - Microseconds (e.g.,
"microsecond","microseconds","μs") - Nanoseconds (e.g.,
"nanosecond","nanoseconds","ns")
How it works
The parser:- Converts the input string to lowercase
- Uses a language-specific regex to match numbers and unit names
- Calculates the total milliseconds by:
- Extracting each (number, unit) pair
- Converting each pair to milliseconds
- Summing all the values
- Returns
0if no valid matches are found
Case insensitivity
The parser is case-insensitive:Decimal values
The parser supports decimal values:Comparison with ms()
Whilems() is the recommended way to parse durations, parseDuration() is useful when:
- You need explicit control over the language parameter
- You’re working with
Languageobjects directly - You want to avoid the automatic language resolution of
ms()
See also
- ms() - The main ms function (recommended)
- formatMilliseconds() - Format milliseconds to strings