Options
All
  • Public
  • Public/Protected
  • All
Menu

A TimeUnit represents time durations at a given unit of granularity and provides utility methods to convert across units, and to perform timing and delay operations in these units.

A TimeUnit does not maintain time information, but only helps organize and use time representations that may be maintained separately across various contexts. A nanosecond is defined as one thousandth of a microsecond, a microsecond as one thousandth of a millisecond, a millisecond as one thousandth of a second, a minute as sixty seconds, an hour as sixty minutes, and a day as twenty four hours.

TimeUnit is an enumeration and in usage the already defined constants should be used:

Example:

// Converting 10 minutes to nanoseconds
MINUTES.toNanos(10)
// Equivalent with the above:
NANOSECONDS.convert(10, MINUTES)

Hierarchy

  • TimeUnit

Index

Properties

Abstract label

label: string

A human readable label for this unit.

Abstract ord

ord: number

A number representing the unit's ordering in the TimeUnit enumeration, useful for doing comparisons to find out which unit is more coarse grained.

MINUTES.ord < DAYS.ord // true
SECONDS.ord > MICROSECONDS.org // true

Methods

Abstract convert

  • convert(duration: number, unit: TimeUnit): number
  • Converts the given time duration in the given unit to this unit. Conversions from finer to coarser granularities truncate, so lose precision. For example, converting 999 milliseconds to seconds results in 0. Conversions from coarser to finer granularities with arguments that would numerically overflow saturate to Number.MAX_VALUE if negative or MAX_VALUE if positive.

    For example, to convert 10 minutes to milliseconds, use:

    MILLISECONDS.convert(10, MINUTES)
    // ... or ...
    MINUTES.toMillis(10)
    

    Parameters

    • duration: number

      the time duration in the given unit

    • unit: TimeUnit

      the unit of the duration argument

    Returns number

    the converted duration in this unit, or Number.MIN_VALUE if conversion would negatively overflow, or Number.MAX_VALUE if it would positively overflow

Abstract toDays

  • toDays(d: number): number
  • Converts the given d value to days.

    Equivalent with DAYS.convert(duration, this).

    Parameters

    • d: number

      is the converted duration

    Returns number

    the converted duration, or Number.MAX_SAFE_INTEGER + 1 (or 2^53) if it overflows, or Number.MIN_SAFE_INTEGER - 1 if it underflows (or -2^53).

Abstract toHours

  • toHours(d: number): number
  • Converts the given d value to hours.

    Equivalent with HOURS.convert(duration, this).

    Parameters

    • d: number

      is the converted duration

    Returns number

    the converted duration, or Number.MAX_SAFE_INTEGER + 1 (or 2^53) if it overflows, or Number.MIN_SAFE_INTEGER - 1 if it underflows (or -2^53).

Abstract toMicros

  • toMicros(d: number): number
  • Converts the given d value to microseconds.

    Equivalent with MICROSECONDS.convert(duration, this).

    Parameters

    • d: number

      is the converted duration

    Returns number

    the converted duration, or Number.MAX_SAFE_INTEGER + 1 (or 2^53) if it overflows, or Number.MIN_SAFE_INTEGER - 1 if it underflows (or -2^53).

Abstract toMillis

  • toMillis(d: number): number
  • Converts the given d value to milliseconds.

    Equivalent with MILLISECONDS.convert(duration, this).

    Parameters

    • d: number

      is the converted duration

    Returns number

    the converted duration, or Number.MAX_SAFE_INTEGER + 1 (or 2^53) if it overflows, or Number.MIN_SAFE_INTEGER - 1 if it underflows (or -2^53).

Abstract toMinutes

  • toMinutes(d: number): number
  • Converts the given d value to minutes.

    Equivalent with MINUTES.convert(duration, this).

    Parameters

    • d: number

      is the converted duration

    Returns number

    the converted duration, or Number.MAX_SAFE_INTEGER + 1 (or 2^53) if it overflows, or Number.MIN_SAFE_INTEGER - 1 if it underflows (or -2^53).

Abstract toNanos

  • toNanos(d: number): number
  • Converts the given d value to nanoseconds.

    Equivalent with NANOSECONDS.convert(duration, this).

    Parameters

    • d: number

      is the converted duration

    Returns number

    the converted duration, or Number.MAX_SAFE_INTEGER + 1 (or 2^53) if it overflows, or Number.MIN_SAFE_INTEGER - 1 if it underflows (or -2^53).

Abstract toSeconds

  • toSeconds(d: number): number
  • Converts the given d value to seconds.

    Equivalent with SECONDS.convert(duration, this).

    Parameters

    • d: number

      is the converted duration

    Returns number

    the converted duration, or Number.MAX_SAFE_INTEGER + 1 (or 2^53) if it overflows, or Number.MIN_SAFE_INTEGER - 1 if it underflows (or -2^53).

toString

  • toString(): string
  • Override for Object.toString.

    Returns string

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc