chronia

endOfYear

Overview

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.

Signature

function endOfYear(date: DateInput): Date;

Parameters

Parameter Type Description
date DateInput A Date object, numeric timestamp, or ISO 8601 string representing the base date

Return Value

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

Description

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.

Specification

Returns a Date set to December 31st, 23:59:59.999 when:

Returns Invalid Date when:

Behavior Notes

Use Cases

Usage Examples

Year Range Queries

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

Year-based Reporting

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

Time Period Calculations

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

Data Validation

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