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

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

The parameters represent:

  • Years

  • Months

  • Weeks

  • Days

  • Hours

  • Minutes

  • Seconds

  • Milliseconds

  • Microseconds

  • Nanoseconds

In this example, the duration is 7 days.


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().

Example

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

const difference = start.until(end);

This returns a Duration representing the time between the two dates.


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.

Next chapter: JavaScript Temporal Date Arithmetic.



×

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.

-->