ts-time/Duration

Consumption

import Duration, {NULL_DURATION, MS_DURATION, SECOND_DURATION, MINUTE_DURATION, HOUR_DURATION, DAY_DURATION, WEEK_DURATION} from "ts-time/Duration";

Default export#

Hierarchy#

  • class Duration

Description#

Comprises a fixed time interval in milliseconds. As opposed to Period, it can't comprise a variable-length interval such as month, because month duration varies from 28 to 31 days. Duration is mainly involved to manipulate Instant objects. In particular, ZonedDateTime.plusDuration method adds a duration to the respective instant, not date/time. It means that if you add DAY_DURATION to a ZonedDateTime, you may receive a different LocalTime if time zone offset changes during this day, while real time difference will truly be 24 hours:

const start = ZonedDateTime.parse("2019-10-27T00:00:00.000+02:00[Europe/Berlin]"), // Summer time
  end = start.plusDuration(DAY_DURATION);
expect(end.toString()).toBe("2019-10-27T23:00:00.000+01:00[Europe/Berlin]"); // Winter time
expect(end.epochMs - start.epochMs).toBe(24 * MS_PER_HOUR);

To ascertain the same LocalTime, you are supposed to add a Period.

Fields#

ms#

readonly ms: number

Duration in milliseconds.

Methods#

plus#

(value: number | Duration): Duration

value
Duration to add, or milliseconds.
returns
Sum of two durations.
minus#

(value: number | Duration): Duration

value
Duration to subtract, or milliseconds.
returns
Difference of two durations.
multiply#

(multiplier: number): Duration

multiplier
Multiplier of the duration.
returns
This duration taken the specified number of times.

Static methods#

of#

(value: number | Duration): Duration

value
Duration as object or number of milliseconds.
returns
Duration instance.
ofMs#

(ms: number): Duration

ms
Duration in milliseconds.
returns
Duration instance.
ofSeconds#

(seconds: number): Duration

ms
Duration in seconds.
returns
Duration instance.
ofMinutes#

(minutes: number): Duration

ms
Duration in minutes.
returns
Duration instance.
ofHours#

(hours: number): Duration

ms
Duration in hours.
returns
Duration instance.
ofDays#

(days: number): Duration

ms
Duration in days.
returns
Duration instance.
ofWeeks#

(weeks: number): Duration

ms
Duration in weeks.
returns
Duration instance.
ofComponents#

(days: number, hours: number = 0, minutes: number = 0, seconds: number = 0, ms: number = 0): Duration

days
Number of days.
hours
Number of hours.
minutes
Number of minutes.
seconds
Number of seconds.
ms
Number of milliseconds.
returns
Duration instance.

NULL_DURATION#

NULL_DURATION: Duration

Duration representing an empty interval (0 milliseconds).

MS_DURATION#

MS_DURATION: Duration

Duration representing a millisecond interval of real time.

SECOND_DURATION#

SECOND_DURATION: Duration

Duration representing a second interval of real time.

MINUTE_DURATION#

MINUTE_DURATION: Duration

Duration representing a minute interval of real time.

HOUR_DURATION#

HOUR_DURATION: Duration

Duration representing an hour interval of real time.

DAY_DURATION#

DAY_DURATION: Duration

Duration representing a day interval of real time.

WEEK_DURATION#

WEEK_DURATION: Duration

Duration representing a week interval of real time.