The endOfYear function returns a new Date object representing the last moment of the year (December 31st at 23:59:59.999) for a given date. It provides a reliable way to obtain year-end boundaries in Chronia’s consistent API surface.
function endOfYear(date: DateInput): Date;
| Parameter | Type | Description |
|---|---|---|
date |
DateInput |
A Date object, numeric timestamp, or ISO 8601 string representing the base date |
| Type | Description |
|---|---|
Date |
A new Date object set to December 31st at 23:59:59.999 of the same year, or Invalid Date if the input is invalid |
The endOfYear function calculates the final moment of the year for the provided Date object or timestamp. It sets the returned date to December 31st with all time components set to their maximum values (23:59:59.999), while preserving the year from the input date. This function is useful for date range calculations, year-based filtering, and time boundary operations.
Date object (not Invalid Date)0, representing January 1, 1970, 00:00:00 UTC)new Date('invalid'))NaNInfinity-InfinityDate | numberstartOfYear, endOfMonth)import { endOfYear, startOfYear } from "chronia";
// Get full year range for 2024
const yearStart = startOfYear(new Date(2024, 5, 15));
const yearEnd = endOfYear(new Date(2024, 5, 15));
// yearStart: January 1, 2024 00:00:00.000
// yearEnd: December 31, 2024 23:59:59.999
// Check if a date is within a specific year
function isInYear(date: Date, year: number): boolean {
const referenceDate = new Date(year, 0, 1);
const start = startOfYear(referenceDate);
const end = endOfYear(referenceDate);
const time = date.getTime();
return time >= start.getTime() && time <= end.getTime();
}
isInYear(new Date(2024, 11, 31, 23, 59, 59), 2024); // Returns: true
isInYear(new Date(2025, 0, 1, 0, 0, 0), 2024); // Returns: false
import { endOfYear } from "chronia";
// Calculate financial year end
function getFiscalYearEnd(date: Date): Date {
return endOfYear(date);
}
const fiscalYearEnd = getFiscalYearEnd(new Date(2024, 3, 15));
// Returns: December 31, 2024 23:59:59.999
// Works with timestamps
const timestamp = Date.now();
const yearEnd = endOfYear(timestamp);
// Returns: December 31st of current year at 23:59:59.999
import { endOfYear } from "chronia";
// Filter events that occur in a specific year
interface Event {
name: string;
date: Date;
}
function getEventsInYear(events: Event[], year: number): Event[] {
const yearEndDate = endOfYear(new Date(year, 0, 1));
const yearEndTime = yearEndDate.getTime();
return events.filter(
(event) =>
event.date.getFullYear() === year && event.date.getTime() <= yearEndTime,
);
}
// Works regardless of leap year
const leapYearEnd = endOfYear(new Date(2024, 1, 29));
// Returns: December 31, 2024 23:59:59.999
const normalYearEnd = endOfYear(new Date(2023, 5, 15));
// Returns: December 31, 2023 23:59:59.999
import { endOfYear, isValid } from "chronia";
// Safe year-end calculation with validation
function safeEndOfYear(date: Date | number): Date | null {
const result = endOfYear(date);
return isValid(result) ? result : null;
}
// Valid date
safeEndOfYear(new Date(2024, 5, 15)); // Returns: December 31, 2024 23:59:59.999
// Invalid date
safeEndOfYear(new Date("invalid")); // Returns: null
// Calculate remaining days in year
function daysRemainingInYear(date: Date): number {
const end = endOfYear(date);
if (!isValid(end)) {
return -1;
}
const diffMs = end.getTime() - date.getTime();
return Math.ceil(diffMs / (1000 * 60 * 60 * 24));
}
daysRemainingInYear(new Date(2024, 0, 1)); // Returns: 365 (leap year)
daysRemainingInYear(new Date(2023, 0, 1)); // Returns: 364