Orders two values of the same kind for Array.prototype.sort(), returning negative, zero, or positive numbers. Works across moments, epoch timestamps, calendar dates, clock times, durations, year-month values, and ranges.
Either argument can also be a raw ISO string, native JavaScript Date, or epoch millisecond timestamp. When compared with a clock time, a number is read as an hour, while comparing different kinds throws a TypeError.
First typed value, ISO string, Date, or epoch millisecond timestamp to compare.
bPoint | Duration | YearMonth | Rangerequired
Second value of the same kind, or a raw input matching a.
Returns
-1 | 0 | 1
Returns -1 when the first value comes earlier, 0 when equal, and 1 when later.
Ranges of the same kind sort by their start point first and break ties by their end point:
import { compare, dateRange } from 'native-dates';const windows = [ dateRange('2026-11-10', '2026-11-20'), dateRange('2026-11-10', '2026-11-14'), dateRange('2026-11-01', '2026-11-05'),];windows.sort(compare).map(String);// ['2026-11-01/2026-11-05', '2026-11-10/2026-11-14', '2026-11-10/2026-11-20']
Comparing moments across time zones
compare() orders DateTime values by their exact instant rather than their local clock digits. A 9:00 AM meeting in New York sorts before an 8:00 AM meeting on the same day in Los Angeles.
Picking boundary points
Find the minimum or maximum value across a set of dates, moments, clock times, or YearMonth values. Both helpers accept rest arguments or a single non-empty array.
earliest()
earliest(...points): DateTime | CalendarDate | Time | YearMonthearliest(points): DateTime | CalendarDate | Time | YearMonth
Returns the earliest value from one or more arguments or a non-empty array of the same kind, converting raw dates or epoch millisecond inputs into a DateTime. Passing an empty array throws a RangeError.
Parameters
pointsPoint[] | YearMonth[]required
One or more dates, moments, epoch timestamps, clock times, or year-months of the same kind.
Returns
DateTime | CalendarDate | Time | YearMonth
The value that comes first. Equal moments keep the earlier item in the list.
import { date, earliest, time } from 'native-dates';earliest(date('2026-11-20'), date('2026-11-04'), date('2026-11-16')).toString(); // '2026-11-04'earliest([time('5:30pm'), time('9:00am'), time('12:15pm')]).toString(); // '09:00:00'earliest(1794850200000, 1794840000000).epochMilliseconds; // 1794840000000
latest()
latest(...points): DateTime | CalendarDate | Time | YearMonthlatest(points): DateTime | CalendarDate | Time | YearMonth
Returns the latest value from one or more arguments or a non-empty array of the same kind. Passing an empty array throws a RangeError.
Parameters
pointsPoint[] | YearMonth[]required
One or more dates, moments, clock times, or year-months of the same kind.
Returns
DateTime | CalendarDate | Time | YearMonth
The value that comes last. Equal moments keep the earlier item in the list.
Combine earliest() and latest() to clamp a value between two bounds: