Yevhen Klymentiev
dark
light
console
darkness
y.klymentiev@gmail.com
Reusable Snippets|Practical utility code for everyday use — custom-built and ready to share

endOfDay

Returns a new Date object set to the end of the given day (23:59:59.999).

TypeScript
Copied!
1/**
2 * Returns a new Date object set to the end of the given day (23:59:59.999).
3 *
4 * @param date - The input date.
5 * @returns A new Date representing the end of the day.
6 */
7export function endOfDay(date: Date): Date {
8  return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59, 999);
9}
  • Precise Daily Upper Bound

    Sets the time to 23:59:59.999, which is the exact last millisecond of the day — ideal for inclusive range comparisons.

  • Immutable

    Returns a new Date instance, preserving the integrity of the input.

  • Consistent Local Time Behavior

    Works in the system's local timezone, aligning with how most user-facing apps handle dates.

  • Minimal & Efficient

    Uses direct construction — no need for cloning or incremental time adjustments.

Tests | Examples

TypeScript
Copied!
1test('endOfDay - sets time to 23:59:59.999', () => {
2  const input = new Date('2023-08-25T10:00:00.000Z');
3  const result = endOfDay(input);
4
5  expect(result.getFullYear()).toBe(2023);
6  expect(result.getMonth()).toBe(7); // August
7  expect(result.getDate()).toBe(25);
8  expect(result.getHours()).toBe(23);
9  expect(result.getMinutes()).toBe(59);
10  expect(result.getSeconds()).toBe(59);
11  expect(result.getMilliseconds()).toBe(999);
12});
13
14test('endOfDay - does not mutate original date', () => {
15  const input = new Date('2023-08-25T00:00:00.000Z');
16  const copy = new Date(input.getTime());
17  endOfDay(input);
18  expect(input).toEqual(copy);
19});

Common Use Cases

  • Date Range Filters

    Define inclusive end boundaries in search or report queries (e.g., filter all events through the end of a specific day).

  • Batch Processing Windows

    Schedule tasks or run analytics through the end of each day without missing late entries.

  • Calendar UI Boundaries

    Highlight or block out full-day selections including the last moment of a day.

  • Validation Logic

    Ensure a timestamp does not exceed the end of a day for business rules or user input validation.

  • Backup & Archival Systems

    Tag or finalize daily datasets with an end-of-day marker.

Codebase: Utilities -> Dates -> endOfDay | Yevhen Klymentiev