Compare and diff
Compare
Section titled “Compare”const day = yuga("2024-05-15");
day.isBefore("2024-06-01"); // trueday.isAfter(new Date(2024, 0, 1)); // trueday.isSame("2024-05-15"); // trueday.isSameOrAfter("2024-05-15", "day"); // trueday.isSameOrBefore("2024-05-31", "month"); // trueThe 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 SundayisSameOrBefore 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"); // 1209600000yuga("2024-05-15").diff("2024-05-01", "day"); // 14yuga("2024-05-15").diff("2024-01-31", "month"); // 3yuga("2024-05-15").diff("2024-01-31", "month", true); // 3.48…, the same fraction as Day.jsMonths 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.