chronia

endOfDay

Overview

The endOfDay function returns a new Date object set to the last moment of the day (23:59:59.999) for a given date. It preserves the calendar date while setting all time components to their maximum values.

Signature

function endOfDay(date: DateInput): Date;

Parameters

Parameter Type Description
date DateInput The base date as a Date object, numeric timestamp, or ISO 8601 string

Return Value

Type Description
Date A new Date object set to 23:59:59.999 of the same date, or Invalid Date if input is invalid

Description

The endOfDay function takes a date input and returns a new Date object representing the last millisecond of that calendar day. The function sets the hours to 23, minutes to 59, seconds to 59, and milliseconds to 999, effectively creating a timestamp for 23:59:59.999 on the same date.

Specification

Returns a valid Date set to 23:59:59.999 when:

Returns Invalid Date when:

Behavior Notes

Use Cases

Usage Examples

Time Range Boundaries

import { endOfDay } from "chronia";

// Create an inclusive date range for a specific day
function getEventsForDay(targetDate: Date, events: Array<{ timestamp: Date }>) {
  const dayEnd = endOfDay(targetDate);

  return events.filter((event) => event.timestamp <= dayEnd);
}

// Example: Get end of a specific day
const june15 = new Date(2024, 5, 15, 14, 30, 45, 123);
const endOfJune15 = endOfDay(june15);
// Returns: Date object for June 15, 2024 23:59:59.999

// Works with any time of day
const morning = new Date(2024, 5, 15, 6, 0, 0, 0);
endOfDay(morning);
// Returns: Date object for June 15, 2024 23:59:59.999

Daily Aggregations

import { endOfDay } from "chronia";

// Calculate daily statistics with precise boundaries
function getDailyStats(
  data: Array<{ timestamp: number; value: number }>,
  day: Date,
) {
  const dayEnd = endOfDay(day).getTime();

  return data
    .filter((item) => item.timestamp <= dayEnd)
    .reduce((sum, item) => sum + item.value, 0);
}

// Works with timestamps
const today = Date.now();
const endOfToday = endOfDay(today);
// Returns: End of today at 23:59:59.999

Deadline Management

import { endOfDay } from "chronia";

// Set end-of-day deadline for task completion
interface Task {
  id: string;
  dueDate: Date;
  completedAt?: Date;
}

function isTaskOverdue(task: Task, currentTime: Date = new Date()): boolean {
  const deadline = endOfDay(task.dueDate);
  return currentTime > deadline && !task.completedAt;
}

// Check if task is still within the due date
const task = {
  id: "123",
  dueDate: new Date(2024, 5, 30),
};

const deadline = endOfDay(task.dueDate);
// Returns: June 30, 2024 23:59:59.999
// Task is due any time before this moment

Date Normalization

import { endOfDay } from "chronia";

// Normalize multiple timestamps from the same day
function normalizeToEndOfDay(timestamps: number[]): Date[] {
  return timestamps.map((ts) => endOfDay(ts));
}

// All times on the same day normalize to the same end-of-day
const timestamps = [
  new Date(2024, 5, 15, 8, 0).getTime(),
  new Date(2024, 5, 15, 14, 30).getTime(),
  new Date(2024, 5, 15, 20, 45).getTime(),
];

const normalized = normalizeToEndOfDay(timestamps);
// All three return: June 15, 2024 23:59:59.999

Error Handling

import { endOfDay } from "chronia";

// Handle invalid inputs gracefully
function safeEndOfDay(date: Date | number): Date | null {
  const result = endOfDay(date);
  return isNaN(result.getTime()) ? null : result;
}

// Valid input
safeEndOfDay(new Date(2024, 5, 15));
// Returns: Date object for June 15, 2024 23:59:59.999

// Invalid input
safeEndOfDay(new Date("invalid"));
// Returns: null (after detecting Invalid Date)

// Invalid number input
safeEndOfDay(NaN);
// Returns: null (after detecting NaN)