Skip to main content

Constructor

constructor(duration: DurationResolvable)
Creates a new HolyDuration instance.
duration
DurationResolvable
required
The duration value in milliseconds. Can be a number or another HolyDuration instance.

Instance Methods

add()

add(duration: HolyDuration): this
add(amount: number, unit?: HumanUnit): this
Adds to this duration (mutates).
duration
HolyDuration
A HolyDuration instance to add
amount
number
The amount to add (when using number overload)
unit
HumanUnit
default:"'milliseconds'"
The unit of time (when using number overload)
return
this
This instance for chaining
Examples:
duration.add(5, 'minutes')
duration.add(otherDuration)

subtract()

subtract(duration: HolyDuration): this
subtract(amount: number, unit?: HumanUnit): this
Subtracts from this duration (mutates).
duration
HolyDuration
A HolyDuration instance to subtract
amount
number
The amount to subtract (when using number overload)
unit
HumanUnit
default:"'milliseconds'"
The unit of time (when using number overload)
return
this
This instance for chaining
Examples:
duration.subtract(30, 'seconds')
duration.subtract(otherDuration)

in()

in(unit: HumanUnit): number
Converts the duration to the specified unit.
unit
HumanUnit
required
The unit to convert to (‘milliseconds’, ‘seconds’, ‘minutes’, ‘hours’, ‘days’, ‘weeks’, ‘months’, ‘years’)
return
number
The duration value in the specified unit
Example:
const duration = new HolyDuration(60000)
duration.in('seconds') // 60
duration.in('minutes') // 1

format()

format(
  type: 'short' | 'long' = 'short',
  allowedSegments: Partial<Record<HumanUnit, boolean>> = { /* all units enabled */ }
): string
Formats the duration as a human-readable string.
type
'short' | 'long'
default:"'short'"
Format type:
  • 'short': Compact format (e.g., “1h 30m 45s”)
  • 'long': Verbose format (e.g., “1 hour, 30 minutes and 45 seconds”)
allowedSegments
Partial<Record<HumanUnit, boolean>>
Object specifying which time units to include in the output. By default, all units are enabled.
return
string
Formatted duration string
Examples:
const duration = new HolyDuration(5430000) // 1h 30m 30s

// Short format (default)
duration.format('short') // "1h 30m 30s"

// Long format
duration.format('long') // "1 hour, 30 minutes and 30 seconds"

// Only show hours and minutes
duration.format('short', { hours: true, minutes: true, seconds: false })
// "1h 30m"

clone()

clone(): HolyDuration
Creates a copy of this HolyDuration instance.
return
HolyDuration
New HolyDuration instance with the same duration value
Example:
const original = new HolyDuration(1000)
const copy = original.clone()
copy.add(500)
// original remains 1000ms

Build docs developers (and LLMs) love