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

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Scope JS Dates JS Temporal  New JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Destructuring JS Data Types JS Errors JS Debugging JS Conventions JS References JS 2026 JS Versions

JS HTML

JS HTML DOM JS Events JS Projects New

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 like 7 days and 1 hour.

The Temporal.Duration object includes these properties:

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds
  • milliseconds
  • nanoseconds

Add and Subtract Time

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

  • How to use JavaScript Temporal.Duration
  • How to to represent and calculate lengths of time
  • Add and subtract days, hours, months, and more safely

Example

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

Temporal Duration Formats

A Duration object can be parsed as a string using the ISO 8601 duration format.

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".

CodeDescription
+Optional +/- sign for positive/negative duration. Default is +.
PDuration designator (for period)
nYNumber of calendar years
nMNumber of calendar months
nWNumber of weeks
nDNumber of calendar days
TTime designator (precedes time components)
nHNumber of hours
nMNumber of minutes
nSNumber of seconds

Create a Duration Using the Constructor

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

Example

In this example, the duration is 7 days and 2 hours.

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

The parameters represent:

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

Create a Duration Using from()

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

Example

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

You can create a Duration using the from() method with an ISO 8601 string:

Example

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

Add a Duration

You can add a Duration to a date using the subtract() method.

The original value does not change.

Example

const date = Temporal.PlainDate.from("2026-05-17");
const duration = Temporal.Duration.from({ days: 10 });

const result = date.add(duration);
Try it Yourself »

Subtract a Duration

You can subtract a Duration using the subtract() method.

Example

const date = Temporal.PlainDate.from("2026-05-17");
const duration = Temporal.Duration.from({ days: 10 });

const result = date.subtract(duration);
Try it Yourself »

Calculate Temporal Differences

You calculate the differnce between two temporal dates using until() or since().

The since() method is effectively the inverse of the until() method.

Or the until() method is the inverse of the since() method.


JavaScript Temporal since()

The since() method calculates the duration between two temporal dates.

Syntax

t1.since(t2, options)

Meaning:

At time t1, how much time has passed since time t2?

Example: Plain Date

const start = Temporal.PlainDate.from("2026-05-01");
const end = Temporal.PlainDate.from("2026-05-17");

const duration = end.since(start);
Try it Yourself »

Example: Plain Time

const start = Temporal.PlainTime.from("09:00");
const end = Temporal.PlainTime.from("17:30");

const duration = end.since(start);
Try it Yourself »

Example: ZonedDateTime

const start = Temporal.ZonedDateTime.from(
"2026-02-17T09:00:00+01:00[Europe/Oslo]");

const end = Temporal.ZonedDateTime.from(
"2026-02-17T17:30:00+01:00[Europe/Oslo]");

const duration = end.since(start);
Try it Yourself »

Note

The since() method returns a Temporal.Duration Object representing the elapsed time.

The duration is positive if the "other" date is in the past.

The duration is negative if the "other" date is in the future.


The Options Parameters

Example

const wedding = Temporal.PlainDate.from('2000-05-17');
const today = Temporal.Now.plainDateISO();

const duration = today.since(wedding);
Try it Yourself »

The since() method returns the total number of days, but you can use the largestUnit option to break it down into years and months:

Example

const wedding = Temporal.PlainDate.from('2026-05-17');
const today = Temporal.Now.plainDateISO();

const duration = today.since(wedding, {largestUnit:'years'});
Try it Yourself »


JavaScript Temporal until()

The until() method calculates the duration between two temporal dates.

The until() method is effectively the inverse of the since() method.

Syntax

t1.until(t2, options)

Meaning:

At time t1, how much time is it until t2?"

Example

Return a Duration representing the time between two dates:

const start = Temporal.PlainDate.from("2026-05-01");
const end = Temporal.PlainDate.from("2026-05-17");

const duration = start.until(end);
Try it Yourself »

Temporal since() vs until()

The methods are opposites.

MethodMeaning
a.since(b)time from b → a
a.until(b)time from a → b

Example

const start = Temporal.PlainDate.from("2026-05-01");
const end = Temporal.PlainDate.from("2026-05-17");

const duration1 = end.since(start);
const duration2 = start.until(end);
Try it Yourself »

Both return the same duration.

Think:

  • since = past
  • Until = future

Examples:

  • since yesterday
  • until tomorrow

The Compare() Method

Example

const duration1 = Temporal.Duration.from({ hours: 1, minutes: 30 });
const duration2 = Temporal.Duration.from({ minutes: 90 });

let text = "The durations are not equal.";
// Compare the durations
if (Temporal.Duration.compare(duration1, duration2) === 0) {
  let text = "The durations are equal.";
}
Try it Yourself »

Note

The Temporal.Duration object does not have an equals() method due to the complexity of duration equality, specifically how to handle different representations of the same duration and calendar math.

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


The width() Method

The width() method creates a new duration with modified time units (years, months, days, etc).

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 »

Duration vs Date Math

With Date, you must often calculate time differences manually using milliseconds.

Date Example

const start = new Date("2026-05-01");
const end = new Date("2026-05-17");

const diff = end - start;
Try it Yourself »

Note

Temporal provides methods that are clearer and safer than using Date methods.


Key Features of Temporal.Duration

  • Precise Representation
    A Temporal.Duration object can handle time in various units, including years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds.
  • Safe Date Arithmetic
    Temporal.Duration facilitates safe and clear date and time arithmetic, preventing issues related to daylight saving time and time zone changes.
  • ISO 8601 Compatibility
    Durations can be created from and converted to ISO 8601 duration strings (e.g., P3D for "3 days").
  • Immutability
    Temporal objects are immutable, meaning operations like add() or subtract() return a new Temporal.Duration instance, leaving the original unchanged.

When to Use Duration

  • Adding or subtracting time.

  • Calculating age.

  • Measuring differences between dates.

  • Working with time spans (hours, days, months).


Temporal.Duration Methods

MethodDescription
from()Returns a new Duration object from another object or a string
Arithmetic
add()Returns a new Duration with a duration added
subtract()Returns a new Duration with a duration subtracted
abs()Returns a new Duration with the absolute value
negated()Returns a new Duration with the negated value
round()Returns a new Duration rounded to a given unit
with()Returns a new Duration with specified fields modified
Comparison
compare()Comparing two times (returning -1, 0, or 1)
Formatting
total()Returns a number representing the duration in a given unit
toString()Returns an ISO 8601 string representation
toJSON()Returns an ISO 8601 string for JSON serialization
toLocaleString()Returns a language-sensitive representation of the time
valueOf()Throws a TypeError (prevents temporals from being converted to primitives)


×

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.

-->