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 Dates 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

Add and Subtract Time

What You Will Learn:

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

Note

A Temporal.Duration represents a length of time.

It can include years, months, weeks, days, hours, minutes, seconds, and more.


Create a Duration

You can create a Duration using numeric values.

Example

In this example, the duration is 7 days.

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

The parameters represent:

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

Create Duration from an Object

You can also create a Duration using an object.

Example

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


Add a Duration

You can add a Duration to a date or date-time.

The original value does not change.

Example

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

const result = date.add(duration);

Subtract a Duration

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

Example

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

const result = date.subtract(duration);
console.log(result);

Calculate the Difference Between Two Dates

You can calculate the duration between two dates using until() or since().

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

It has the following form (spaces are only 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

JavaScript Temporal since()

The since() method calculates the duration between two temporal date/time values.

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

Syntax

temporal.since(temporal, options)

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

The duration is positive if the "other" date is in the past, or negative if it is in the future:

Example

const wedding = Temporal.PlainDate.from('2026-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 date/time values.

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

Syntax

temporal.until(temporal, options)

Examples

Return a Duration representing the time between 2026-05-17 and now:

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

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

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 difference = start.until(end);
Try it Yourself »

Note

The since() method does this - other.

The until() method does other - this.


Duration vs Date Math

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

Date Example

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

const diff = end - start;

Temporal provides built-in methods that are clearer and safer.


When to Use Duration

  • Adding or subtracting time.

  • Calculating age.

  • Measuring differences between dates.

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


Summary

Temporal.Duration represents a length of time.

It makes date arithmetic clear, readable, and safer than using manual millisecond calculations.



×

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.

-->