parseMilliseconds() function converts a duration in milliseconds into an object containing individual time units (years, days, hours, etc.).
Import
Signature
The duration in milliseconds to parse
Which units should be calculated. Units not included will be
null in the result.An object with time unit keys and their calculated values. Excluded units have
null values.Usage
Basic parsing
Parse milliseconds into all available units:Selective unit parsing
Specify which units to include in the calculation:Parse specific units only
Get just the units you care about:ParseUnit type
The available unit keys are:How it works
The function works by dividing the total milliseconds by each unit’s value (largest to smallest) and tracking the remainder:- Divide by the unit’s millisecond value
- Store the integer part as the unit count
- Use the remainder for the next unit
- 90061000 ÷ 86400000 (day) = 1 day, remainder 3661000
- 3661000 ÷ 3600000 (hour) = 1 hour, remainder 61000
- 61000 ÷ 60000 (minute) = 1 minute, remainder 1000
- 1000 ÷ 1000 (second) = 1 second, remainder 0
When to use this function
UseparseMilliseconds() when:
- You need the raw breakdown of time units
- Building custom formatting logic
- Displaying individual unit values separately
- You want precise control over which units to calculate
formatMilliseconds() or ms() are more convenient.
Related exports
parseUnits constant
TheparseUnits constant object contains the millisecond values for each unit:
units constant exported from the main package, but only includes the units used in formatting (excludes week, month, decade, century, millennium).
See also
- ms() - The main formatting function
- formatMilliseconds() - Format milliseconds to human-readable strings
- Time constants - Predefined time unit constants
- Types - ParseUnit type definition