Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Basic JavaScript

JS Tutorial JS Introduction JS Where To JS Output

JS Syntax

JS Syntax JS Statements JS Comments JS Variables JS Let JS Const JS Types

JS Operators

JS Operators

JS If Else

JS If Conditions

JS Loops

JS Loops

JS Strings

JS Strings

JS Numbers

JS Numbers

JS Functions

JS Functions

JS Objects

JS Objects

JS Scope

JS Scope

JS Dates

JS Dates

JS Temporal

JS Temporal  New

JS Arrays

JS Arrays

JS Sets

JS Sets

JS Maps

JS Maps

JS Iterations

JS Loops

JS Math

JS Math

JS RegExp

JS RegExp

JS DataTypes

JS Data Types

JS Errors

JS Errors

JS Debugging

JS Debugging

JS Conventions

JS Style Guide

JS Reference

JS Statements

JS Projects

JS Projects New

JS Versions

JS 2026

JS HTML

JS HTML DOM JS Events

JS Advanced

JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web APIs JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


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, microseconds and nanoseconds.

Example

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

Result

P7DT2H
Try it Yourself »

ISO 8601 Duration Codes

An ISO 8601 duration represent lengths of time.

It uses a standard format starting with ±P (Period), followed by the structure nYnMnDTnHnMnS.

Syntax

(spaces are added for readability)

+P nY nM nW nD T nH nM nS

It covers years nY, months nM, weeks nW, days nD, and a T before hours nH, minutes nM, and seconds nS.

Example

Three years, six months, four days, twelve hours, thirty minutes, and five seconds.

P3Y6M4DT12H30M5S

More Examples

DurationCode
1 DayP1D
45 MinutesPT45M
1 Year, 2 Months, 3 DaysP1Y2M3D
4 Hours, 5 Minutes, 6 SecondsPT4H5M6S
2.5 HoursPT2H30M
5 WeeksP5W

How to Create a Temporal.Duration

A Duration can be created in several different ways:

FromCode
With Constructornew Temporal.Duration()
From Object Temporal.Duration.from()
From ISO StringTemporal.Duration.from()

Create a Duration Using new

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

Example

Create a duration of 7 days and 2 hours.

// Create a Duration object
const duration = new Temporal.Duration(0, 0, 0, 7, 2);
Try it Yourself »

The parameters must be in this orde:

  • Years
  • Months
  • Weeks
  • Days
  • Hours
  • Minutes
  • Seconds
  • Milliseconds
  • Microseconds
  • Nanoseconds

Create a Duration Using an Object

You can create a duration object using the from() method with an object literal parameter like {days:7, hours: 2}:

Example

// Create a Duration object
const duration = Temporal.Duration.from({days:7, hours:2});
Try it Yourself »

An object literal is the most common method for creating JavaScript objects.

You create an object by literally writing its contents as key:value pairs enclosed in curly braces { }.


Create a Duration Using an ISO string

You can create a Duration object using the from() method with an ISO duration string as parameter.

Example

// Create a Duration
const duration = Temporal.Duration.from("P7DT2H");
Try it Yourself »

Learn More:

ISO Duration Strings


Temporal.Duration Properties

The Temporal.Duration object has 12 properties of time information.

Example

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

// List the Properties
let text =
"blank: " + duration.blank + " " +
"sign: " + duration.sign + " " +
"years: " + duration.years + " " +
"months: " + duration.months + " " +
"weeks: " + duration.weeks + " " +
"days: " + duration.days + " " +
"hour: " + duration.hours + " " +
"minutes: " + duration.minutes + " " +
"seconds: " + duration.seconds + " " +
"milliseconds: " + duration.milliseconds + " " +
"microseconds: " + duration.microseconds + " " +
"nanoseconds: " + duration.nanoseconds;
Try it Yourself »

Safe Date Arithmetic

The Duration object facilitates safe and clear date and time arithmetic, preventing issues related to daylight saving time and time zone changes.

The Duration object can handle time in various units, including years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds.

The Duration object makes date arithmetic clear, readable, and safer than using manual millisecond calculations.


The Duration add() Method

The add() method returns a new duration with a duration added.

The add() method does not change the original duration.

Example

// Create a Duration
const d1 = Temporal.Duration.from({ hours:2, minutes:30 });

// Add a Duration
const d2 = d1.add({ hours:1, minutes:30 });
Try it Yourself »

The Duration subtract() Method

The subtract() method returns a new duration with a duration subtracted.

The subtract() method does not change the original duration.

Example

// Create a Duration
const d1 = Temporal.Duration.from({ hours:2, minutes:30 });

// Subtract a Duration
const d2 = d1.subtract({ hours:1, minutes:30 });
Try it Yourself »

Immutability

Temporal objects are immutable, meaning operations like add() or subtract() return a new Temporal.Duration instance, leaving the original unchanged.



Temporal add() and subtract()

All temporal objects have their own add() method:

  • duration.add(duration)
  • instant.add(duration)
  • plaindate.add(duration)
  • plaintime.add(duration)
  • plainyearmonth.add(duration)
  • plainmonthday.add(duration)
  • plaindatetime.add(duration)
  • zoneddatetime.add(duration)

All temporal objects have their own subtract() method:

  • duration.subtract(duration)
  • instant.subtract(duration)
  • plaindate.subtract(duration)
  • plaintime.subtract(duration)
  • plainyearmonth.subtract(duration)
  • plainmonthday.subtract(duration)
  • plaindatetime.subtract(duration)
  • zoneddatetime.subtract(duration)

Learn More:

Temporal Arithmetic


The Compare() Method

The Temporal.Duration object does not have an equals() method due to the complexity of handling different representations of the same duration.

Instead, equality is checked using the static Temporal.Duration.compare() method.

This method returns -1 if the first duration is shorter, 0 if it is the same, and 1 if it is longer.

Examples

// Create two Durations
const d1 = Temporal.Duration.from({ hours:1, minutes:30 });
const d2 = Temporal.Duration.from({ minutes:90 });

// Compare the Durations
let result = Temporal.Duration.compare(d1, d2);
Try it Yourself »
// Create two Durations
const d1 = Temporal.Duration.from({ hours:1, minutes:30 });
const d2 = Temporal.Duration.from({ hours:1, minutes:30 });

// Compare the Durations
let result = Temporal.Duration.compare(d1, d2);
Try it Yourself »

Note that both examples return 0 for equal.

90 minutes is the same duration as 1 hour and 30 minutes.


The with() Method

The with() method returns a new duration with specific time units replaced:

Example

// Create a Duration
const duration = Temporal.Duration.from({hours:10, minutes:30});

// Create a new duration with the minutes changed to 45
const newDuration = duration.with({minutes:45});
Try it Yourself »

When to Use Duration

  • Adding or subtracting time

  • Calculating age

  • Calculating differences between dates

  • Calculating time spans (hours, days, months)


Temporal.Duration Methods

ConstructingDescription
from() Returns a new duration object from an object or an ISO string
new() Returns a new duration object from integer parameters
Arithmetic
abs()Returns a new duration with the absolute value of this duration
add()Returns a new duration with a duration added to this duration
negated()Returns a new duration with this duration negated
round()Returns a new duration with this duration rounded
subtract()Returns a new duration with a duration subtracted from this duration
Comparing
compare() Compares two durations (returning -1, 0, or 1)
Converting
with()Returns a new duration with specified field(s) modified
Formatting
total()Returns a number representing the duration in a given unit
toJSON()Returns an RFC 9557 format string for JSON serialization
toLocaleString()Returns a language-sensitive representation of the time
toString()Returns an RFC 9557 format string representation
valueOf()Throws a TypeError (prevents temporals from being converted to primitives)

Temporal.Duration Properties

PropertyDescription
blankBoolean true if the duration represents a zero duration
daysDays as an integer (1-31)
hoursHours as an integer (0-23)
microsecondsMicroseconds as an integer (0-999)
millisecondsMilliseconds as an integer (0-999)
minutesMinutes as an integer (0-59)
monthsMonths as an integer (1-12)
nanosecondsNanoseconds as an integer (0-999)
secondsSeconds as an integer (0-59)
sign1 positive -1 negative
weeksWeeks as an integer
yearsYears as an integer

Display All Duration Properties

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

// List the Properties
let text =
"blank: " + duration.blank + " " +
"sign: " + duration.sign + " " +
"years: " + duration.years + " " +
"months: " + duration.months + " " +
"weeks: " + duration.weeks + " " +
"days: " + duration.days + " " +
"hour: " + duration.hours + " " +
"minutes: " + duration.minutes + " " +
"seconds: " + duration.seconds + " " +
"milliseconds: " + duration.milliseconds + " " +
"microseconds: " + duration.microseconds + " " +
"nanoseconds: " + duration.nanoseconds;
Try it Yourself »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->