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 Compare

Date Comparison

In JavaScript, objects cannot be compared using operators like <, >, ==, or ===.

Always use the equals() or compare() methods rather than standard equality operators.

All temporal objects have their own compare() method:

  • Temporal.Instant.compare(instant1, instant2)
  • Temporal.PlainDate.compare(plaindate1, plaindate2)
  • Temporal.PlainTime.compare(plaintime1, plaintime2)
  • Temporal.PlainYearMonth.compare(plainyearmonth1, plainyearmonth2)
  • Temporal.PlainMonthDay.compare(plainmonthday1 plainmonthday2)
  • Temporal.PlainDateTime.compare(plaindatetime1,plaindatetime2)
  • Temporal.ZonedDateTime.compare(zoneddtatime1, zoneddtatime2)
  • Temporal.Duration.compare(duration1, duration2)

The compare() Method

The compare() method returns:

  • -1 if the first date is earlier
  • 1 if the first day is later
  • 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);
Try it Yourself »

Why < and > Do Not Work

Temporal objects are objects, not primitive numbers.

When you write (a < b), JavaScript tries to convert both objects to primitives.

For Temporal objects, this does not produce a numeric timestamp like Dates.

Example

// Create two Temporal objects
const a = Temporal.PlainDate.from("2026-02-17");
const b = Temporal.PlainDate.from("2026-03-01");

// Compare the dates
console.log(a < b); // ❌ Error

Temporal.Duration.compare()

The Temporal.Duration object does not have an equals() method due to the complexity of handling different representations of the same duration.

Instead, equality is checked using the static Temporal.Duration.compare() method.

Examples

// Create two Durations
const d1 = Temporal.Duration.from({ hours:1, minutes:30 });
const d2 = Temporal.Duration.from({ minutes:90 });

// Compare the Durations
let result = Temporal.Duration.compare(d1, d2);
Try it Yourself »
// Create two Durations
const d1 = Temporal.Duration.from({ hours:1, minutes:30 });
const d2 = Temporal.Duration.from({ hours:1, minutes:30 });

// Compare the Durations
let result = Temporal.Duration.compare(d1, d2);
Try it Yourself »

Note that the two examples above both return 0 for equal.

90 minutes is the same duration as 1 hour and 30 minutes.


Temporal.Instant.compare()

Example

// Create Temporal.Instant objects
const i1 = Temporal.Instant.from("2026-05-17T12:00:00Z");
const i2 = Temporal.Instant.from("2026-05-17T12:00:00Z");
const i3 = Temporal.Instant.from("2026-05-17T13:00:00Z");

// compare()
let x1 = Temporal.Instant.compare(i1, i2));
let x2 = Temporal.Instant.compare(i1, i3));
let x3 = Temporal.Instant.compare(i3, i1));
Try it Yourself »

Temporal.PlainDate.compare()

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);
Try it Yourself »

Temporal Sort

The compare() method is designed to be passed directly into the JavaScript Array.sort() method:

Example

// Create an Array of dates
const dates = [
  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);
Try it Yourself »


Date Comparison

Always use the compare() or equals() methods rather than standard equality operators.


The Temporal equals() Method

Most (*) temporal objects have their own equals() method:

  • instant.equals(instant)
  • plaindate.equals(plaindate)
  • plaintime.equals(plaintime)
  • plainyearmonth.equals(plainyearmonth)
  • plainmonthday.equals(plainmonthday)
  • plaindateTime.equals(plaindatetime)
  • zoneddateTime.equals(zoneddtatime)

(*) Duration has not.

The equals() method returns true if both dates are equal.


Temporal.Instant equals()

Example

// Create Temporal.Instant objects
const i1 = Temporal.Instant.from("2026-05-17T12:00:00Z");
const i2 = Temporal.Instant.from("2026-05-17T12:00:00Z");
const i3 = Temporal.Instant.from("2026-05-17T13:00:00Z");

// equals()
let x1 = i1.equals(i2); // true
let x2 = i1.equals(i3); // false
Try it Yourself »

Temporal.PlainDate equals()

Example

// Create two Temporal objects
const date1 = Temporal.PlainDate.from('2026-05-17');
const date2 = Temporal.PlainDate.from('2026-05-17');

let result = date1.equals(date2);
Try it Yourself »

Temporal.PlainDateTime equals()

You can compare PlaneDateTime values using the equals() method.

Example

const d1 = Temporal.PlainDateTime.from("2026-05-17T14:30:00");
const d2 = Temporal.PlainDateTime.from("2026-05-17T14:30:00");

let result = d1.equals(d2)
Try it Yourself »

×

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.

-->