CyrefJS
CCyrefJS
فا
Back to documentation
Date/Since 1.38.0

differenceInDays

Calculates the difference in calendar days between two dates by comparing them at the start of their respective days.

differenceInDays(dateLeft, dateRight):number

Parameters

dateLeftDaterequired

The first date used as the left side of the difference calculation.

dateRightDaterequired

The second date used as the right side of the difference calculation.

Returns

number

The difference between the two dates in days. Positive values mean dateLeft is later than dateRight, while negative values mean dateLeft is earlier.

Examples

01

Basic usage

Calculate the number of days between two dates.

TypeScript
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);
// 5
02

Negative difference

If dateLeft is earlier than dateRight, the result is negative.

TypeScript
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);
// -5
03

Same date

Two dates representing the same calendar day return zero.

TypeScript
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);
// 0
04

Time of day is ignored

The time components of both dates are reset to midnight before calculating the difference.

TypeScript
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);
// 1
05

Date objects are not modified

The function creates copies of both dates before resetting their time components, leaving the original Date objects unchanged.

TypeScript
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
Built with@cyref/js·Official documentation