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


JS Temporal Arithmetic

Add and Subtract Dates Safely

The Temporal API provides methods for easy and reliable date and time arithmetic.

Add and subtract days, months, years, and time without modifying the original value.

Perform date arithmetic without DST bugs Time Zone problems.

JavaScript Temporal Add and Subtract

Both methods accept an object with duration properties { days: 7, hours: 1 } as input.

Both methods handles date boundaries: adding one day to March 31st is April 1st.

Both methods are immutable, returning new Temporal objects.

The add() method returns a temporal object moved forward by a given duration.

Syntax

temporal.add(duration)

The subtract() method returns a temporal object moved backward by a given duration.

Syntax

temporal.subtract(duration)

Add Days to a PlainDate

Use the add() method to add days.

Example

// Create a PlainDate object
const myDate = Temporal.PlainDate.from('2026-05-17');

// Add a duration
const newDate = myDate.add({ days: 7 });
Try it Yourself »

The original date is not changed.


Subtract Days from a PlainDate

Use subtract() to subtract time.

Example

// Create a PlainDate object
const myDate = Temporal.PlainDate.from('2026-05-17');

// Subtract a duration
const newDate = myDate.subtract({ days: 7 });
Try it Yourself »

Supported Units

You can add or subtract various time units using a duration object:

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

Add Multiple Units

Example

// Create a PlainDate object
const myDate = Temporal.PlainDate.from('2026-05-17');

// Add multiple units
const newDate = today.add({ years: 1, months: 2, days: 15 });
Try it Yourself »

Add Months

Temporal automatically handles different month lengths.

Example

const date = Temporal.PlainDate.from("2026-01-31");

const result = date.add({ months: 1 });
Try it Yourself »

If the next month has fewer days, Temporal adjusts automatically.


Add Years

Adding years works correctly, even for leap years.

Example

const date = Temporal.PlainDate.from("2024-02-29");

const result = date.add({ years: 1 });
Try it Yourself »

Temporal handles leap year adjustments automatically.



Add a Duration to an Istant

From a Temporal.Instant you can only add or subtract a fixed duration (hours, minutes, seconds) but not calendar durations like months or years, as their length can vary depending on the time zone and the calendar.

Example

// Create a Temporal.Instant object
const now = Temporal.Instant.fromEpochMilliseconds(Date.now());

// Subtract 5 hours and 30 minutes
const fiveHalfHoursAgo = now.subtract({ hours: 5, minutes: 30 });
Try it Yourself »

Add a Duration to Now

Example

// Create a Temporal object
const today = Temporal.Now.plainDateISO();

// Add a duration
const nextWeek = today.add({ days: 7 });
Try it Yourself »

Unlike the old Date object, Temporal objects are immutable.

Methods like add(), subtract() or until() always return a new instance rather than modifying the existing one.


Date Arithmetic with ZonedDateTime

ZonedDateTime handles daylight saving time (DST) safely.

Example

const start = Temporal.ZonedDateTime.from
("2026-03-29T00:00:00+01:00[Europe/Oslo]");

const nextDay = start.add({ days: 1 });
Try it Yourself »

If a DST change occurs, Temporal adjusts automatically.


Compare with Date Arithmetic

JavaScript legacy Date can cause leap year errors:

Example

// Create a Date object
const start = new Date("2026-02-17");

// Add 12 days
start.setDate(start.getDate() + 12);

Result:

2026-02-29
Try it Yourself »

2026 is not a leap year.

2026 is not divisible by 4.

The closest leap years are 2024 and 2028.


Best Practices

  • Use PlainDate for date-only arithmetic.

  • Use ZonedDateTime for time zone-aware calculations.

  • Avoid manual millisecond calculations.

  • Prefer immutable operations.


×

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.

-->