Duration

represents a duration of time with support for various units (nanoseconds, microseconds, milliseconds, seconds, minutes, hours, and days).

It provides methods to create, manipulate, and compare durations, as well as convert them to different units and formats.

Example Usage
// Create a duration of 5 seconds
const duration = Duration.seconds(5);

// Convert the duration to minutes
const minutes = duration.inWholeMinutes();

console.log(minutes);
// Output: 0

// Add another duration of 2 minutes
const sum = duration.add(Duration.minutes(2));

console.log(sum.inWholeSeconds());
// Output: 420

Main functionalities

  • Create a duration object from different units of time (nanoseconds, microseconds, milliseconds, seconds, minutes, hours, and days)

  • Convert the duration to different units (seconds, minutes, hours, and days)

  • Perform arithmetic operations on durations (addition, subtraction, multiplication, division)

  • Compare durations (equality, less than, greater than)

  • Format the duration as a string representation

Global Functions

durationOf

Creates a Duration object from a duration in milliseconds.

Static Functions

nanoseconds

Creates a Duration object from a duration in nanoseconds.

microseconds

Creates a Duration object from a duration in microseconds.

milliseconds

Creates a Duration object from a duration in milliseconds.

seconds

Creates a Duration object from a duration in seconds.

minutes

Creates a Duration object from a duration in minutes.

hours

Creates a Duration object from a duration in hours.

days

Creates a Duration object from a duration in days.

Class Functions

inWholeSeconds

Converts the duration to seconds.

inWholeMinutes

Converts the duration to minutes.

inWholeHours

Converts the duration to hours.

inWholeDays

Converts the duration to days.

toObject

Converts the duration to an object with properties for each time unit.

toComponents

Splits this duration into days, hours, minutes, seconds, and nanoseconds.

add

Adds another duration to this duration.

subtract

Subtracts another duration from this duration.

multiply

Multiplies this duration by a scalar factor.

divide

Divides this duration by a scalar divisor.

equals

Checks if this duration is equal to another duration.

lessThan

Checks if this duration is less than another duration.

greaterThan

Checks if this duration is greater than another duration.

compareTo

Compares this duration with another duration.

runIt

Splits this duration into days, hours, minutes, seconds, and nanoseconds and executes the given action with these components. The result of the action is returned.

toString

Formats the duration as a string.

Last updated