Overview
The Duration Humanizer plugin adds ahumanize static method to the Atemporal factory, allowing conversion of Temporal.Duration objects into human-readable, localized strings like “2 hours and 30 minutes” or “3 días y 5 horas”.
Installation
Load the plugin using eitherextend or lazyLoad:
Methods
atemporal.humanize(durationLike, options?)
Converts a Temporal.Duration or duration-like object into a human-readable string.
Signature:
durationLike: ATemporal.Durationinstance or plain object like{ hours: 2, minutes: 30 }options(optional): Configuration object for formatting
Options
TheHumanizeOptions interface provides customization for the output:
locale
The locale to use for formatting. Defaults to 'en'.
Examples:
listStyle
Controls how multiple duration parts are joined together. Uses Intl.ListFormat styles.
'long'(default): Full conjunction style'short': Abbreviated conjunction style'narrow': Minimal conjunction style
unitDisplay
Controls how units are displayed. Uses Intl.NumberFormat unit display styles.
'long'(default): Full unit names (“2 hours”)'short': Abbreviated unit names (“2 hr”)'narrow': Minimal unit names (“2h”)
Supported Duration Units
The plugin processes duration units in the following order:- years
- months
- weeks
- days
- hours
- minutes
- seconds
- milliseconds
Input Formats
Temporal.Duration Object
Duration-Like Plain Object
Single Unit
Zero Duration
Localization Examples
Spanish (es)
French (fr)
German (de)
Japanese (ja)
Chinese (zh)
Supported Locales
The plugin includes enhanced unit mappings for:- English (
en) - Spanish (
es) - French (
fr) - German (
de) - Italian (
it) - Portuguese (
pt) - Russian (
ru) - Japanese (
ja) - Korean (
ko) - Chinese (
zh)
Intl.NumberFormat will work, but the above have optimized translations.
Advanced Examples
Multiple Units
Combining Options
Fractional Values
From Time Difference
Creating Durations with Temporal
Performance
The plugin includes an LRU cache (max 200 entries) that stores formatted duration results. This significantly improves performance for repeated calls with the same parameters.Cache Management
Error Handling
The plugin includes comprehensive error handling:- Invalid or empty durations return
"0 seconds" - Unsupported units are handled with fallback formatting
- Intl API errors trigger fallback to simple string concatenation
- All errors are logged to console with
console.warn()
Fallback Behavior
If theIntl APIs fail (e.g., unsupported locale), the plugin provides intelligent fallbacks:
- Tries to use cached
Intl.NumberFormatwith unit formatting - Falls back to localized unit names from internal mapping
- Falls back to basic English pluralization
- Ultimate fallback returns simple number + unit string
Best Practices
- Specify locale: Always provide a locale for consistent localized output
- Choose appropriate display: Use
narrowfor compact UIs,longfor readable text - Filter zero values: Only include non-zero duration units
- Cache benefits: Reuse the same duration values to benefit from caching
- Validate input: Ensure duration objects are valid before humanizing
- Round appropriately: Consider rounding sub-second values for readability