bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/JavaScript/Objects, Classes, and Advanced Patterns
JavaScript•Objects, Classes, and Advanced Patterns

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 and Time Zone problems. JavaScript Temporal Add and Subtract add() returns a temporal object moved forward by a given duration. Syntax temporal.add( duration )

Example

Formula

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

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

Example

Formula

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

Both methods handles date boundaries

Adding one day to March 31st is April 1st. Both methods are immutable, returning new Temporal objects.

Supported Units

The add()

and subtract() methods accept a duration object as input.

Example:

{ months: 2, days: 7, hours: 1 }.

The following units are supported:

years months weeks days hours minutes seconds milliseconds microseconds nanoseconds

Add or Subtract Time to PlainDateTime

You can safely add or subtract time. The original value does not change.

Example

Formula

// Create a PlainDateTime object const date = Temporal.PlainDateTime.from("2026 - 05 - 17T14:30:00");
// Add and subtract time const earlier = dateTime.subtract({ minutes: 30 });
const later = dateTime.add({ hours: 2 });

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');

Formula

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

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');

Formula

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

Add Multiple Units

Example

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

Formula

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

Add Months

Temporal automatically handles different month lengths.

Example

const date = Temporal.PlainDate.from("2026-01-31");
const result = date.add({ months: 1 });

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

Add Years

Adding years works correctly, even for leap years.

Previous

JavaScript Objects - Advanced

Next

JavaScript Object Definitions