bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/JavaScript/Objects, Classes, and Advanced Patterns
JavaScript•Objects, Classes, and Advanced Patterns

JavaScript Temporal Duration

The Temporal.Duration Object

The

Temporal.Duration object represents a length of time.

Example:

7 days and 1 hour.

The

Temporal.Duration object includes these properties

years, months, weeks, days, hours, minutes, seconds, milliseconds, and nanoseconds.

Example

Formula

// Create a duration using an object literal const duration = Temporal.Duration.from({days:7, hours:2});

ISO 8601 Compatibility

Durations can be created from and converted to

ISO 8601 duration strings

(e.g."P7DT2H"). It has the following form (spaces are added for readability): +P nY nM nW nD T nH nM nS For example, "P3Y6M4DT12H30M5S" represents a duration of "three years, six months, four days, twelve hours, thirty minutes, and five seconds".

Temporal Duration Format Codes

Code

Description

±

Formula

Optional positive/negative duration (default is +).

P Duration designator (for period) nY

Number of calendar years nM

Number of calendar months nW

Number of weeks nD

Number of calendar days

T Time designator (precedes time components) nH

Number of hours nM

Number of minutes nS

Number of seconds

Learn More:

JavaScript Temporal Formats

Create a Duration Using the Constructor

You can create a Temporal.Duration object using the new constructor with integer parameters.

Example

Create a duration of 7 days and 2 hours.

const duration = new Temporal.Duration(0, 0, 0, 7, 2);

The parameters represent:

Years

Months

Previous

JavaScript Object Methods

Next

JavaScript Generators