Class Duration

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

Example

// Example 1
const oneYearInMinutes = (1).years().toHours().toMinutes();

console.log(`1 year is approximately ${oneYearInMinutes} minutes.`);

// Example 2
const duration = (1).years().add((6).months()).toString();
console.log(duration); // Output: 548d 0h 0m 0s 0ns

Since

version 1.1.0

Author

Manuel Santos ney.br.santos@gmail.com

Constructors

  • Creates a new Duration object with the specified duration in milliseconds.

    Parameters

    • milliseconds: number

      The duration in milliseconds.

    Returns Duration

    Example

    const duration = new Duration(5000);
    

Properties

milliseconds: number

Methods

  • Adds another duration to this duration.

    Parameters

    • other: Duration

      The other duration to add.

    Returns Duration

    A new Duration object representing the sum.

    Example

    const duration1 = Duration.hours(12);
    const duration2 = Duration.minutes(30);
    const sum = duration1.add(duration2);
    console.log(sum.toMinutes());
    // Output: 750
  • Compares this duration with another duration.

    Parameters

    • other: Duration

      The other duration to compare.

    Returns number

    -1 if this duration is less than the other, 0 if they are equal, 1 if this duration is greater.

    Example

    const duration1 = Duration.days(1);
    const duration2 = Duration.hours(24);
    console.log(duration1.compareTo(duration2)); // Output: 0
  • Divides this duration by a scalar divisor.

    Parameters

    • divisor: number

      The scalar divisor.

    Returns Duration

    A new Duration object representing the quotient.

    Throws

    Thrown if the divisor is zero.

    Example

    const duration = Duration.hours(1);
    const divided = duration.divide(2);
    console.log(divided.toMinutes());
    // Output: 30
  • Checks if this duration is equal to another duration.

    Parameters

    • other: Duration

      The other duration to compare.

    Returns boolean

    True if the durations are equal, false otherwise.

    Example

    const duration1 = Duration.hours(5);
    const duration2 = Duration.hours(5);
    console.log(duration1.equals(duration2)); // Output: true
  • Checks if this duration is greater than another duration.

    Parameters

    • other: Duration

      The other duration to compare.

    Returns boolean

    True if this duration is greater than the other, false otherwise.

  • Converts the duration to days.

    Returns number

    The duration in days.

    Example

    const duration = Duration.hours(48);
    const days = duration.inWholeDays();
    console.log(days);
    // Output: 2
  • Converts the duration to hours.

    Returns number

    The duration in hours.

    Example

    const duration = Duration.days(1);
    const hours = duration.inWholeHours();
    console.log(hours);
    // Output: 24
  • Converts the duration to minutes.

    Returns number

    The duration in minutes.

    Example

    const duration = Duration.hours(2);
    const minutes = duration.inWholeMinutes();
    console.log(minutes);
    // Output: 120
  • Converts the duration to seconds.

    Returns number

    The duration in seconds.

    Example

    const duration = Duration.minutes(2);
    const seconds = duration.inWholeSeconds();
    console.log(seconds);
    // Output: 120
  • Checks if this duration is less than another duration.

    Parameters

    • other: Duration

      The other duration to compare.

    Returns boolean

    True if this duration is less than the other, false otherwise.

    Example

    const duration1 = Duration.seconds(10);
    const duration2 = Duration.seconds(20);

    console.log(duration1.lessThan(duration2)); // Output: true
  • Multiplies this duration by a scalar factor.

    Parameters

    • factor: number

      The scalar factor.

    Returns Duration

    A new Duration object representing the product.

    Example

    const duration = Duration.minutes(10);
    const multiplied = duration.multiply(2);
    console.log(multiplied.toMinutes());
    // Output: 20
  • 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.

    Type Parameters

    • T = any

    Parameters

    • action: ((days, hours, minutes, seconds, nanoseconds) => T)

      The action to be executed with the duration components.

        • (days, hours, minutes, seconds, nanoseconds): T
        • Parameters

          • days: number
          • hours: number
          • minutes: number
          • seconds: number
          • nanoseconds: number

          Returns T

    Returns T

    The result of the action.

    Example

    const duration = Duration.seconds(3600);
    const result = duration.runIt((days, hours, minutes, seconds, nanoseconds) => {
    return `${days}d ${hours}h ${minutes}m ${seconds}s ${nanoseconds}ns`;
    });
    console.log(result);
    // Output: 0d 1h 0m 0s 0ns
  • Subtracts another duration from this duration.

    Parameters

    • other: Duration

      The other duration to subtract.

    Returns Duration

    A new Duration object representing the difference.

    Example

    const duration1 = Duration.hours(12);
    const duration2 = Duration.minutes(30);
    const difference = duration1.subtract(duration2);
    console.log(difference.toMinutes());
    // Output: 630
  • Splits this duration into days, hours, minutes, seconds, and nanoseconds.

    Returns {
        days: number;
        hours: number;
        minutes: number;
        nanoseconds: number;
        seconds: number;
    }

    An object with properties for days, hours, minutes, seconds, and nanoseconds.

    • days: number
    • hours: number
    • minutes: number
    • nanoseconds: number
    • seconds: number

    Example

    const duration = Duration.days(2).add(Duration.hours(12));
    const components = duration.toComponents();
    console.log(components);
    // Output: { days: 2, hours: 12, minutes: 0, seconds: 0, nanoseconds: 0 }
  • Converts the duration to an object with properties for each time unit.

    Returns {
        days: number;
        hours: number;
        microseconds: number;
        milliseconds: number;
        minutes: number;
        nanoseconds: number;
        seconds: number;
    }

    An object with properties for nanoseconds, microseconds, milliseconds, seconds, minutes, hours, and days.

    • days: number
    • hours: number
    • microseconds: number
    • milliseconds: number
    • minutes: number
    • nanoseconds: number
    • seconds: number

    Example

    const duration = Duration.seconds(123);
    const durationObject = duration.toObject();
    console.log(durationObject);
    // Output: { nanoseconds: 123000000000, microseconds: 123000000, milliseconds: 123000, seconds: 123, minutes: 2, hours: 0, days: 0 }
  • Formats the duration as a string.

    Returns string

    • The formatted string representation of the duration.

    Example

    const duration = Duration.days(1).add(Duration.hours(6));
    const formattedString = duration.toString();
    console.log(formattedString);
    // Output: 1d 6h 0m 0s 0ns
  • Creates a Duration object from a duration in microseconds.

    Parameters

    • microseconds: number

      The duration in microseconds.

    Returns Duration

    A Duration object.

    Example

    const duration = Duration.microseconds(5000000);
    
  • Creates a Duration object from a duration in milliseconds.

    Parameters

    • milliseconds: number

      The duration in milliseconds.

    Returns Duration

    A Duration object.

    Example

    const duration = Duration.milliseconds(5000);
    
  • Creates a Duration object from a duration in nanoseconds.

    Parameters

    • nanoseconds: number

      The duration in nanoseconds.

    Returns Duration

    A Duration object.

    Example

    const duration = Duration.nanoseconds(1e9);
    

Generated using TypeDoc