Overview

A helpful builder class for manually creating new DataAPIDuration instances.

Provides methods for incrementally adding duration components, and negating the final result.

Should be instantiated using DataAPIDuration.builder/duration.builder.

Usage

You may call each .add*() method any number of times, in any order, before calling build.

The .negate(sign?) method may be called at any time to negate the final result.

  • You may pass a boolean to .negate(sign?) to definitively set the sign
  • Otherwise, the sign will be toggled.

A base duration may be provided to initialize the builder with its components and its sign.

Example

const base = duration('1y');

// '-1y3d15h'
const span = duration.builder(base)
.addHours(10)
.addDays(3)
.addHours(5)
.negate()
.build();

See

DataAPIDuration

Constructors

Properties

_days: number = 0
_index: number = -1
_months: number = 0
_nanoseconds: bigint = ...
_negative: boolean = false
validateOrder: boolean = false

Methods

  • Overview

    Adds the given number of days to this DataAPIDurationBuilder.

    If the total number of days exceeds 2147483647 (2^31 - 1), a RangeError is thrown.

    The days may be negative to perform a subtraction operation, but if the total number of days becomes negative, a RangeError also is thrown. To negate the final result, use the .negate() method.

    Parameters

    • days: number

      The number of days to add

    Returns this

    The mutated builder instance

    Example

    // '14d'
    const span = duration.builder()
    .addDays(14)
    .build();

    // true
    duration('2w').equals(span)
  • Overview

    Adds the given number of hours to this DataAPIDurationBuilder.

    Hours are converted to nanoseconds before being added (1 hour = 3,600,000,000,000 nanoseconds).

    If the total number of nanoseconds exceeds 9223372036854775807n (2^63 - 1), a RangeError is thrown.

    The hours may be negative to perform a subtraction operation, but if the total number of nanoseconds becomes negative, a RangeError also is thrown. To negate the final result, use the .negate() method.

    Parameters

    • hours: number | bigint

      The number of hours to add

    Returns this

    The mutated builder instance

    Example

    // '10h'
    const span = duration.builder()
    .addHours(10)
    .build();

    // true
    duration('600m').equals(span)
  • Overview

    Adds the given number of microseconds to this DataAPIDurationBuilder.

    Microseconds are converted to nanoseconds before being added (1 microsecond = 1,000 nanoseconds).

    If the total number of nanoseconds exceeds 9223372036854775807n (2^63 - 1), a RangeError is thrown.

    The microseconds may be negative to perform a subtraction operation, but if the total number of nanoseconds becomes negative, a RangeError also is thrown. To negate the final result, use the .negate() method.

    Parameters

    • microseconds: number | bigint

      The number of microseconds to add

    Returns this

    The mutated builder instance

    Example

    // '1000us'
    const span = duration.builder()
    .addMicros(1000)
    .build();

    // true
    duration('1ms').equals(span)
  • Overview

    Adds the given number of milliseconds to this DataAPIDurationBuilder.

    Milliseconds are converted to nanoseconds before being added (1 millisecond = 1,000,000 nanoseconds).

    If the total number of nanoseconds exceeds 9223372036854775807n (2^63 - 1), a RangeError is thrown.

    The milliseconds may be negative to perform a subtraction operation, but if the total number of nanoseconds becomes negative, a RangeError also is thrown. To negate the final result, use the .negate() method.

    Parameters

    • milliseconds: number | bigint

      The number of milliseconds to add

    Returns this

    The mutated builder instance

    Example

    // '1000ms'
    const span = duration.builder()
    .addMillis(1000)
    .build();

    // true
    duration('1s').equals(span)
  • Overview

    Adds the given number of minutes to this DataAPIDurationBuilder.

    Minutes are converted to nanoseconds before being added (1 minute = 60,000,000,000 nanoseconds).

    If the total number of nanoseconds exceeds 9223372036854775807n (2^63 - 1), a RangeError is thrown.

    The minutes may be negative to perform a subtraction operation, but if the total number of nanoseconds becomes negative, a RangeError also is thrown. To negate the final result, use the .negate() method.

    Parameters

    • minutes: number | bigint

      The number of minutes to add

    Returns this

    The mutated builder instance

    Example

    // '10m'
    const span = duration.builder()
    .addMinutes(10)
    .build();

    // true
    duration('600s').equals(span)
  • Overview

    Adds the given number of months to this DataAPIDurationBuilder.

    If the total number of months exceeds 2147483647 (2^31 - 1), a RangeError is thrown.

    The months may be negative to perform a subtraction operation, but if the total number of months becomes negative, a RangeError also is thrown. To negate the final result, use the .negate() method.

    Parameters

    • months: number

      The number of months to add

    Returns this

    The mutated builder instance

    Example

    // '24mo'
    const span = duration.builder()
    .addMonths(24)
    .build();

    // true
    duration('2y').equals(span)
  • Overview

    Adds the given number of nanoseconds to this DataAPIDurationBuilder.

    If the total number of nanoseconds exceeds 9223372036854775807n (2^63 - 1), a RangeError is thrown.

    The nanoseconds may be negative to perform a subtraction operation, but if the total number of nanoseconds becomes negative, a RangeError also is thrown. To negate the final result, use the .negate() method.

    Parameters

    • nanoseconds: number | bigint

      The number of nanoseconds to add

    Returns this

    The mutated builder instance

    Example

    // '1000ns'
    const span = duration.builder()
    .addNanos(1000)
    .build();

    // true
    duration('1us').equals(span)
  • Overview

    Adds the given number of seconds to this DataAPIDurationBuilder.

    Seconds are converted to nanoseconds before being added (1 second = 1,000,000,000 nanoseconds).

    If the total number of nanoseconds exceeds 9223372036854775807n (2^63 - 1), a RangeError is thrown.

    The seconds may be negative to perform a subtraction operation, but if the total number of nanoseconds becomes negative, a RangeError also is thrown. To negate the final result, use the .negate() method.

    Parameters

    • seconds: number | bigint

      The number of seconds to add

    Returns this

    The mutated builder instance

    Example

    // '10s'
    const span = duration.builder()
    .addSeconds(10)
    .build();

    // true
    duration('10000ms').equals(span)
  • Overview

    Adds the given number of weeks to this DataAPIDurationBuilder.

    Weeks are converted to days before being added (1 week = 7 days).

    If the total number of days exceeds 2147483647 (2^31 - 1), a RangeError is thrown.

    The weeks may be negative to perform a subtraction operation, but if the total number of days becomes negative, a RangeError also is thrown. To negate the final result, use the .negate() method.

    Parameters

    • weeks: number

      The number of weeks to add

    Returns this

    The mutated builder instance

    Example

    // '2w'
    const span = duration.builder()
    .addWeeks(2)
    .build();

    // true
    duration('14d').equals(span)
  • Overview

    Adds the given number of years to this DataAPIDurationBuilder.

    Years are converted to months before being added (1 year = 12 months).

    If the total number of months exceeds 2147483647 (2^31 - 1), a RangeError is thrown.

    The years may be negative to perform a subtraction operation, but if the total number of months becomes negative, a RangeError also is thrown. To negate the final result, use the .negate() method.

    Parameters

    • years: number

      The number of years to add

    Returns this

    Example

    // '2y'
    const span = duration.builder()
    .addYears(1)
    .addYears(1)
    .build();

    // true
    duration('24mo').equals(span)
  • Overview

    Builds a new DataAPIDuration instance from the components added to this DataAPIDurationBuilder.

    May be called at any time to retrieve the current state of the builder as a DataAPIDuration.

    Returns DataAPIDuration

    A new DataAPIDuration instance derived from this DataAPIDurationBuilder

    Example

    const builder = duration
    .builder()
    .addYears(1);

    // '1y'
    const span1 = builder.build().toString();

    builder
    .negate()
    .addMonths(1);

    // '-1y1mo'
    const span2 = builder.build().toString();
  • Overview

    Clones this DataAPIDurationBuilder instance.

    The cloned instance will have the same components as this one, but will be a separate object.

    const builder = duration.builder().addYears(1);
    const clone = builder.clone();
    builder.addMonths(1);

    // '1y'
    clone.build().toString();

    // '1y1mo'
    builder.build().toString();

    Returns DataAPIDurationBuilder

    A new DataAPIDurationBuilder instance with the same components as this one

  • Overview

    Negates the final result of this DataAPIDurationBuilder.

    A boolean parameter may be provided to force the sign to be negative/positive—otherwise, it defaults to toggling the sign.

    Note that negation does not take place until the .build() method is called. It simply marks the final result as to-be-negated or not.

    Parameters

    • negative: boolean = !this._negative

      Whether to set the sign to negative; defaults to the opposite of the current sign

    Returns this

    The mutated builder instance

    Example

    // '-10h'
    const span = duration.builder()
    .addHours(10)
    .negate()
    .build();

    // '10h'
    const span = duration.builder()
    .addHours(10)
    .negate(true)
    .negate(false)
    .build();