Skip to content

Compare and diff

const day = yuga("2024-05-15");
day.isBefore("2024-06-01"); // true
day.isAfter(new Date(2024, 0, 1)); // true
day.isSame("2024-05-15"); // true
day.isSameOrAfter("2024-05-15", "day"); // true
day.isSameOrBefore("2024-05-31", "month"); // true

The second argument compares by a unit: two instances are the same month when they share the month in the first instance’s time zone.

yuga("2024-05-13").isSame("2024-05-12", "week"); // true in en, the week starts on Sunday

isSameOrBefore and isSameOrAfter are plugins in Day.js and part of core in yuga. isBetween and isToday are extensions: see Extensions and plugins.

diff is the time from another moment to this one, in milliseconds by default, or in a unit, truncated towards zero:

yuga("2024-05-15").diff("2024-05-01"); // 1209600000
yuga("2024-05-15").diff("2024-05-01", "day"); // 14
yuga("2024-05-15").diff("2024-01-31", "month"); // 3
yuga("2024-05-15").diff("2024-01-31", "month", true); // 3.48…, the same fraction as Day.js

Months and years are counted on the calendar, the same as Day.js and Moment.js. With { exact: true } they are Temporal’s calendar arithmetic instead:

yuga("2024-05-15").diff("2024-01-31", "month", { exact: true });

A diff with an invalid instance is NaN in every unit.