The diffMonths function calculates the difference in calendar months between two dates. It considers only the year and month components of dates, ignoring day and time values entirely.
function diffMonths(dateLeft: DateInput, dateRight: DateInput): number;
| Parameter | Type | Description |
|---|---|---|
dateLeft |
DateInput |
The first date as a Date object, numeric timestamp, or ISO 8601 string |
dateRight |
DateInput |
The second date as a Date object, numeric timestamp, or ISO 8601 string |
| Type | Description |
|---|---|
number |
The difference in calendar months. Positive if dateLeft is after dateRight, negative if before, or NaN if any input is invalid |
The diffMonths function computes the number of full calendar months between two dates by examining only their year and month values. Day and time components (hours, minutes, seconds, milliseconds) are completely ignored in the calculation. This makes it ideal for calculating month-based differences where the specific day doesn’t matter.
dateLeft represents a later month/year than dateRightdiffMonths(new Date(2024, 5, 1), new Date(2024, 2, 1)) returns 3diffMonths(new Date(2024, 5, 30), new Date(2024, 5, 1)) returns 0dateLeft represents an earlier month/year than dateRightdiffMonths(new Date(2024, 2, 1), new Date(2024, 5, 1)) returns -3NaN when:dateLeft or dateRight is an Invalid Date object (e.g., new Date('invalid'))NaNInfinity or -Infinity(yearDiff * 12) + monthDiffNaN for invalid inputs rather than throwing errorsDate | number types are acceptedNaN for invalid inputs (unlike boolean functions which return false or comparison functions which throw errors)import { diffMonths } from "chronia";
// Calculate months in a subscription period
const subscriptionStart = new Date(2024, 0, 15); // January 15, 2024
const subscriptionEnd = new Date(2024, 11, 20); // December 20, 2024
const monthsSubscribed = diffMonths(subscriptionEnd, subscriptionStart);
// Returns: 11
// Calculate months remaining
const currentDate = new Date(2024, 5, 10); // June 10, 2024
const monthsRemaining = diffMonths(subscriptionEnd, currentDate);
// Returns: 6
import { diffMonths } from "chronia";
// Calculate age in months
const birthDate = new Date(2020, 2, 15); // March 15, 2020
const today = new Date(2024, 5, 20); // June 20, 2024
const ageInMonths = diffMonths(today, birthDate);
// Returns: 51
// Months since project started
const projectStart = new Date(2023, 8, 1); // September 1, 2023
const monthsSinceStart = diffMonths(today, projectStart);
// Returns: 9
import { diffMonths } from "chronia";
// Days are ignored in calculation
const date1 = new Date(2024, 5, 30); // June 30, 2024
const date2 = new Date(2024, 5, 1); // June 1, 2024
const result = diffMonths(date1, date2);
// Returns: 0 (same month and year)
// Works across year boundaries
const endOfYear = new Date(2024, 11, 31); // December 31, 2024
const startOfYear = new Date(2024, 0, 1); // January 1, 2024
const monthsInYear = diffMonths(endOfYear, startOfYear);
// Returns: 11
import { diffMonths } from "chronia";
// Using numeric timestamps
const timestamp1 = new Date(2025, 2, 15).getTime();
const timestamp2 = new Date(2024, 2, 15).getTime();
const monthsDiff = diffMonths(timestamp1, timestamp2);
// Returns: 12
// Mixed Date and timestamp
const dateObj = new Date(2024, 6, 10);
const timestampNum = new Date(2024, 3, 5).getTime();
const result = diffMonths(dateObj, timestampNum);
// Returns: 3
import { diffMonths } from "chronia";
// Invalid Date returns NaN
const invalidDate = new Date("invalid");
const validDate = new Date(2024, 5, 15);
const result = diffMonths(invalidDate, validDate);
// Returns: NaN
// Check for valid result
function calculateMonthDifference(date1: Date, date2: Date): number | null {
const diff = diffMonths(date1, date2);
return isNaN(diff) ? null : diff;
}
// Usage with validation
const diff = calculateMonthDifference(
new Date(2024, 5, 1),
new Date(2024, 2, 1),
);
// Returns: 3
const invalidDiff = calculateMonthDifference(
new Date("invalid"),
new Date(2024, 2, 1),
);
// Returns: null
import { diffMonths } from "chronia";
// Earlier date as first parameter gives negative result
const earlier = new Date(2024, 2, 15); // March 15, 2024
const later = new Date(2024, 5, 20); // June 20, 2024
const result = diffMonths(earlier, later);
// Returns: -3
// Useful for determining past vs future
function getMonthsUntilEvent(eventDate: Date): number {
const today = new Date();
const months = diffMonths(eventDate, today);
if (months > 0) {
return months; // Event is in the future
} else if (months < 0) {
return Math.abs(months); // Event was in the past
} else {
return 0; // Event is this month
}
}