A time-based amount of time, such as '34.5 seconds'.

This class models a quantity or amount of time in terms of seconds and nanoseconds. It can be accessed using other duration-based units, such as minutes and hours. In addition, the DAYS unit can be used and is treated as exactly equal to 24 hours, thus ignoring daylight savings effects.

A physical duration could be of infinite length. The duration uses nanosecond resolution with a maximum value of the seconds that can be held in a long. This is greater than the current estimated age of the universe.

The range of a duration requires the storage of a number larger than a long. To achieve this, the class stores a long representing seconds and an integer representing nanosecond-of-second, which will always be between 0 and 999,999,999. The model is of a directed duration, meaning that the duration may be negative.

The duration is measured in "seconds", but these are not necessarily identical to the scientific "SI second" definition based on atomic clocks. This difference only impacts durations measured near a leap-second and should not affect most applications.

This is a value-based class; use of identity-sensitive operations on instances of Duration may have unpredictable results and should be avoided. The equals method should be used for comparisons.

Properties

nanos: number

the number of nanoseconds

seconds: number

the number of seconds

FOREVER: Duration = ...

A constant for a duration of forever.

ZERO: Duration = ...

A constant for a duration of zero.

Methods

  • Compares this duration to the specified duration.

    Parameters

    • other: Duration

      the duration being compared to this duration

    Returns number

    a negative value if this duration is less than the specified duration, a positive value if this duration is greater than the specified duration, or zero if the two durations are equal.

  • Creates a new Duration instance by subtracting the specified number of days from this duration.

    Parameters

    • daysToSubtract: number

      the number of days to subtract

    Returns Duration

    a new Duration instance with the difference of this duration and the specified number of days.

  • Creates a new Duration instance by subtracting the specified number of hours from this duration.

    Parameters

    • hoursToSubtract: number

      the number of hours to subtract

    Returns Duration

    a new Duration instance with the difference of this duration and the specified number of hours.

  • Creates a new Duration instance by subtracting the specified number of milliseconds from this duration.

    Parameters

    • millisToSubtract: number

      the number of milliseconds to subtract

    Returns Duration

    a new Duration instance with the difference of this duration and the specified number of milliseconds.

  • Creates a new Duration instance by subtracting the specified number of minutes from this duration.

    Parameters

    • minutesToSubtract: number

      the number of minutes to subtract

    Returns Duration

    a new Duration instance with the difference of this duration and the specified number of minutes.

  • Creates a new Duration instance by subtracting the specified number of nanoseconds from this duration.

    Parameters

    • nanosToSubtract: number

      the number of nanoseconds to subtract

    Returns Duration

    a new Duration instance with the difference of this duration and the specified number of nanoseconds.

  • Creates a new Duration instance by subtracting the specified number of seconds from this duration.

    Parameters

    • secondsToSubtract: number

      the number of seconds to subtract

    Returns Duration

    a new Duration instance with the difference of this duration and the specified number of seconds.

  • Creates a new Duration instance by adding the specified number of days to this duration.

    Parameters

    • daysToAdd: number

      the number of days to add

    Returns Duration

    a new Duration instance with the sum of this duration and the specified number of days.

  • Creates a new Duration instance by adding the specified number of hours to this duration.

    Parameters

    • hoursToAdd: number

      the number of hours to add

    Returns Duration

    a new Duration instance with the sum of this duration and the specified number of hours.

  • Creates a new Duration instance by adding the specified number of milliseconds to this duration.

    Parameters

    • millisToAdd: number

      the number of milliseconds to add

    Returns Duration

    a new Duration instance with the sum of this duration and the specified number of milliseconds.

  • Creates a new Duration instance by adding the specified number of minutes to this duration.

    Parameters

    • minutesToAdd: number

      the number of minutes to add

    Returns Duration

    a new Duration instance with the sum of this duration and the specified number of minutes.

  • Creates a new Duration instance by adding the specified number of nanoseconds to this duration.

    Parameters

    • nanosToAdd: number

      the number of nanoseconds to add

    Returns Duration

    a new Duration instance with the sum of this duration and the specified number of nanoseconds.

  • Creates a new Duration instance by adding the specified number of seconds to this duration.

    Parameters

    • secondsToAdd: number

      the number of seconds to add

    Returns Duration

    a new Duration instance with the sum of this duration and the specified number of seconds.

  • Creates a new Duration instance with the specified number of nanoseconds. The current number of seconds in this duration is preserved.

    Parameters

    • nanos: number

      the number of nanoseconds for the new duration

    Returns Duration

    a new Duration instance with the specified number of nanoseconds.

  • Creates a new Duration instance with the specified number of seconds. The current number of nanoseconds in this duration is preserved.

    Parameters

    • seconds: number

      the number of seconds for the new duration

    Returns Duration

    a new Duration instance with the specified number of seconds.

  • Creates a new Duration instance representing the specified number of days.

    Parameters

    • days: number

      the number of days

    Returns Duration

    a new Duration instance representing the specified number of days.

  • Creates a new Duration instance representing the specified number of hours.

    Parameters

    • hours: number

      the number of hours

    Returns Duration

    a new Duration instance representing the specified number of hours.

  • Creates a new Duration instance representing the specified number of milliseconds.

    Parameters

    • millis: number

      the number of milliseconds

    Returns Duration

    a new Duration instance representing the specified number of milliseconds.

  • Creates a new Duration instance representing the specified number of minutes.

    Parameters

    • minutes: number

      the number of minutes

    Returns Duration

    a new Duration instance representing the specified number of minutes.

  • Creates a new Duration instance representing the specified number of nanoseconds.

    Parameters

    • nanos: number

      the number of nanoseconds

    Returns Duration

    a new Duration instance representing the specified number of nanoseconds.

  • Creates a new Duration instance representing the specified number of seconds.

    Parameters

    • seconds: number

      the number of seconds

    Returns Duration

    a new Duration instance representing the specified number of seconds.

  • Creates a new Duration instance representing the specified number of seconds adjusted by a number of nanoseconds.

    Parameters

    • seconds: number

      the number of seconds

    • nanoAdjustment: number

      the number of nanoseconds by which to adjust the seconds

    Returns Duration

    a new Duration instance representing the specified number of seconds adjusted by the specified number of nanoseconds.