bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

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

JS Temporal Differences

Calculate Temporal Differences

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

The since()

method is the inverse of the until() method.

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

Example: Plain Time const start = Temporal.PlainTime.from("09:00");

const end = Temporal.PlainTime.from("17:30");
const duration = end.since(start);

The Options Parameters

Example

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

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

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

Temporal since() vs until()

The methods are opposites.

Method

Meaning 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);
Both return the same duration.

Think:

Formula

since = past
Until = future

Examples:

since yesterday until tomorrow

The compare() Method

Formula

The compare() method returns - 1 if the first date is earlier, 1 if it is later, and 0 if they are equal:

Example

// Create two Temporal objects const date1 = Temporal.PlainDate.from("2026-05-17");
const date2 = Temporal.PlainDate.from("2024-12-25");
// Compare the dates result = Temporal.PlainDate.compare(date1, date2);
The compare() method is designed to be passed directly into the JavaScript Array.sort() method:

Example

// Create an Array of dates const dates = [

Formula

Temporal.PlainDate.from("2026 - 05 - 17"),
Temporal.PlainDate.from("2022 - 01 - 01"),
Temporal.PlainDate.from("2024 - 12 - 25")
];
// Sort chronologically dates.sort(Temporal.PlainDate.compare);

Date Comparison

Previous

JavaScript Object Definitions

Next

this in JavaScript Objects