Options
All
  • Public
  • Public/Protected
  • All
Menu

Represents an ISO 8601 duration.

Here we add a day.

import { Duration } from '@jetblack/date'

const duration = new Duration('P1D')
const d1 = addDuration(new Date('2000-01-01T00:00:00Z'), duration)
console.log(d1)
// Sun Jan 02 2000 00:00:00 GMT+0000 (Greenwich Mean Time)

Note that durations may be negative.

import { Duration } from '@jetblack/date'

const duration = new Duration('-P1D')
const d1 = addDuration(new Date('2000-01-01T00:00:00Z'), duration)
console.log(d1)
// Fri Dec 31 1999 00:00:00 GMT+0000 (Greenwich Mean Time)

Hierarchy

  • Duration

Index

Constructors

  • new Duration(years: number, months: number, weeks?: number, days?: number, hours?: number, minutes?: number, seconds?: number): Duration
  • new Duration(value: string | number | Duration): Duration
  • Constructs a duration.

    The resulting duration will be simplified. For example 12 months will become 1 year and 0 months.

    From component values.

    const duration = new Duration(2, 14, 0, 0, 24)
    console.log(duration.toString())
    // P3Y2M1D

    From a string.

    const duration = new Duration('P2Y14MT24H')
    console.log(duration.toString())
    // P3Y2M1D

    From a value.

    const duration = new Duration(98582400)
    console.log(duration.toString())
    // P3Y2M1D

    Parameters

    • years: number

      The years.

    • months: number

      The months.

    • Optional weeks: number

      The weeks.

    • Optional days: number

      The days.

    • Optional hours: number

      The hours.

    • Optional minutes: number

      The minutes.

    • Optional seconds: number

      The seconds.

    Returns Duration

  • Constructs a year from the string representation, the number of seconds, or another duration object.

    The resulting duration will be simplified. For example 12 months will become 1 year and 0 months.

    From component values.

    const duration = new Duration(2, 14, 0, 0, 24)
    console.log(duration.toString())
    // P3Y2M1D

    From a string.

    const duration = new Duration('P2Y14MT24H')
    console.log(duration.toString())
    // P3Y2M1D

    From a value.

    const duration = new Duration(98582400)
    console.log(duration.toString())
    // P3Y2M1D

    Parameters

    • value: string | number | Duration

      The duration as a string, number, or duration.

    Returns Duration

Accessors

  • get days(): number
  • set days(value: number): void
  • Gets the days.

    Returns number

  • Sets the days.

    If the value is greater than or equal to 7 the duration is simplified.

    Parameters

    • value: number

    Returns void

  • get hours(): number
  • set hours(value: number): void
  • Gets the hours.

    Returns number

  • Sets the hours.

    If the value is greater than or equal to 24 the duration is simplified.

    Parameters

    • value: number

    Returns void

  • get minutes(): number
  • set minutes(value: number): void
  • Gets the minutes.

    Returns number

  • Sets the minutes.

    If the value is greater than or equal to 60 the duration is simplified.

    Parameters

    • value: number

    Returns void

  • get months(): number
  • set months(value: number): void
  • Gets the months.

    Returns number

  • Sets the months.

    If the value is greater than or equal to 12 the duration is simplified.

    Parameters

    • value: number

    Returns void

  • get seconds(): number
  • set seconds(value: number): void
  • Gets the seconds.

    Returns number

  • Sets the seconds.

    If the value is greater than or equal to 60 the value is simplified.

    Parameters

    • value: number

    Returns void

  • get weeks(): number
  • set weeks(value: number): void
  • get years(): number
  • set years(value: number): void

Methods

  • [toPrimitive](hint: string): any
  • toString(): string
  • Creates a string representation of the duration.

    Returns string

    An ISO 8601 duration.

  • valueOf(): number
  • The number of milliseconds of the duration assuming a year has 360 days and a month has 30 days.

    Note that this is not idempotent. As the actual number of days in a year is not available, constant values are used. This means that 30 days is equivalent to a month.

    const d1 = new Duration('P1M')
    console.log(d1.valueOf())
    // 2592000

    const d2 = new Duration('P30D')
    console.log(d1.valueOf())
    // 2592000

    Returns number

    The number of milliseconds to which the duration corresponds.

Generated using TypeDoc