The diffYears function calculates the difference in calendar years between two dates. It provides a simple year-based calculation that ignores months, days, and time components, focusing solely on the year values.
function diffYears(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 years (negative if dateLeft is before dateRight), or NaN if any input is invalid |
The diffYears function determines the number of full calendar years between two dates by comparing only their year components. This function is part of Chronia’s suite of difference calculation utilities and provides a simplified year-based comparison that ignores all finer temporal granularity.
The calculation is straightforward: dateLeft.getFullYear() - dateRight.getFullYear(). This means that regardless of the month, day, or time values, only the year difference is computed.
dateLeft has a year value greater than dateRightdiffYears(new Date(2024, 0, 1), new Date(2020, 11, 31)) returns 40 when:diffYears(new Date(2024, 11, 31), new Date(2024, 0, 1)) returns 0dateLeft has a year value less than dateRightdiffYears(new Date(2020, 5, 15), new Date(2024, 5, 15)) returns -4NaN when:dateLeft or dateRight is an Invalid Date object (e.g., new Date('invalid'))NaNInfinity or -InfinitydiffYears(new Date("invalid"), new Date(2024, 5, 15)) returns NaNNaN for invalid inputs rather than throwing exceptions, following Chronia’s graceful error handling pattern for calculation functionsisValidDateOrNumber validator to ensure input validity before performing calculationsdiffYears(a, b) === -diffYears(b, a)import { diffYears } from "chronia";
// Calculate approximate age (ignores birth month/day)
const birthDate = new Date(1990, 5, 15); // June 15, 1990
const today = new Date(2024, 10, 22); // November 22, 2024
const age = diffYears(today, birthDate);
// Returns: 34
// Works with timestamps
const birthTimestamp = new Date(1990, 5, 15).getTime();
const currentTimestamp = Date.now();
const ageFromTimestamp = diffYears(currentTimestamp, birthTimestamp);
// Returns: 34 (as of 2024)
import { diffYears } from "chronia";
interface Record {
id: string;
createdAt: Date;
data: any;
}
// Filter records older than 5 years
function filterOldRecords(records: Record[]): Record[] {
const now = new Date();
return records.filter((record) => {
const yearDiff = diffYears(now, record.createdAt);
return yearDiff > 5;
});
}
// Example usage
const records: Record[] = [
{ id: "1", createdAt: new Date(2015, 3, 10), data: {} },
{ id: "2", createdAt: new Date(2023, 7, 20), data: {} },
{ id: "3", createdAt: new Date(2018, 11, 5), data: {} },
];
const oldRecords = filterOldRecords(records);
// Returns records from 2015 and 2018 (as of 2024)
import { diffYears } from "chronia";
// Calculate project duration in years
const projectStart = new Date(2019, 0, 1);
const projectEnd = new Date(2024, 11, 31);
const projectYears = diffYears(projectEnd, projectStart);
// Returns: 5
// Same year calculation
const sameYearStart = new Date(2024, 0, 1);
const sameYearEnd = new Date(2024, 11, 31);
const duration = diffYears(sameYearEnd, sameYearStart);
// Returns: 0 (both in same year)
import { diffYears } from "chronia";
// Compare historical events
const eventA = new Date(1776, 6, 4); // July 4, 1776
const eventB = new Date(1969, 6, 20); // July 20, 1969
const yearsBetween = diffYears(eventB, eventA);
// Returns: 193
// Handles century boundaries
const year1899 = new Date(1899, 11, 31);
const year1900 = new Date(1900, 0, 1);
const centuryDiff = diffYears(year1900, year1899);
// Returns: 1
import { diffYears } from "chronia";
// Invalid date handling
const invalidDate = new Date("invalid");
const validDate = new Date(2024, 5, 15);
const result1 = diffYears(invalidDate, validDate);
// Returns: NaN
const result2 = diffYears(validDate, invalidDate);
// Returns: NaN
// Check for valid result
function safeYearDiff(date1: Date, date2: Date): number | null {
const diff = diffYears(date1, date2);
return isNaN(diff) ? null : diff;
}
const safeDiff = safeYearDiff(invalidDate, validDate);
// Returns: null