1.38.0differenceInDays
Calculates the difference in calendar days between two dates by comparing them at the start of their respective days.
differenceInDays(dateLeft, dateRight):numberParameters
dateLeftDaterequiredThe first date used as the left side of the difference calculation.
dateRightDaterequiredThe second date used as the right side of the difference calculation.
Returns
numberThe difference between the two dates in days. Positive values mean dateLeft is later than dateRight, while negative values mean dateLeft is earlier.
Examples
Basic usage
Calculate the number of days between two dates.
import { differenceInDays } from "@cyref/js";
const dateLeft = new Date("2026-01-10");
const dateRight = new Date("2026-01-05");
const result = differenceInDays(dateLeft, dateRight);
console.log(result);
// 5Negative difference
If dateLeft is earlier than dateRight, the result is negative.
import { differenceInDays } from "@cyref/js";
const dateLeft = new Date("2026-01-05");
const dateRight = new Date("2026-01-10");
const result = differenceInDays(dateLeft, dateRight);
console.log(result);
// -5Same date
Two dates representing the same calendar day return zero.
import { differenceInDays } from "@cyref/js";
const dateLeft = new Date("2026-03-15");
const dateRight = new Date("2026-03-15");
const result = differenceInDays(dateLeft, dateRight);
console.log(result);
// 0Time of day is ignored
The time components of both dates are reset to midnight before calculating the difference.
import { differenceInDays } from "@cyref/js";
const dateLeft = new Date("2026-04-10T23:30:00");
const dateRight = new Date("2026-04-09T01:15:00");
const result = differenceInDays(dateLeft, dateRight);
console.log(result);
// 1Date objects are not modified
The function creates copies of both dates before resetting their time components, leaving the original Date objects unchanged.
import { differenceInDays } from "@cyref/js";
const dateLeft = new Date("2026-05-10T18:30:00");
const dateRight = new Date("2026-05-05T09:15:00");
differenceInDays(dateLeft, dateRight);
console.log(dateLeft);
// 2026-05-10T18:30:00
console.log(dateRight);
// 2026-05-05T09:15:00