chronia

isSameDay

Overview

The isSameDay function checks whether two given dates fall on the same calendar day, ignoring time components. It provides a reliable way to compare dates by calendar day in Chronia’s consistent API surface.

Signature

function isSameDay(dateLeft: DateInput, dateRight: DateInput): boolean;

Parameters

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

Return Value

Type Description
boolean Returns true if both dates are on the same calendar day, false otherwise

Description

The isSameDay function determines whether two dates represent the same calendar day by comparing them at midnight, effectively ignoring all time components (hours, minutes, seconds, milliseconds). It leverages Chronia’s diffDays function internally, returning true when the day difference is exactly zero.

Specification

Returns true when:

Returns false when:

Behavior Notes

Use Cases

Usage Examples

Date Equality Checks

import { isSameDay } from "chronia";

// Same day, different times
const morning = new Date(2024, 5, 15, 9, 0, 0);
const evening = new Date(2024, 5, 15, 23, 59, 59);
isSameDay(morning, evening); // Returns: true

// Different days
const today = new Date(2024, 5, 15, 23, 59);
const tomorrow = new Date(2024, 5, 16, 0, 0);
isSameDay(today, tomorrow); // Returns: false

// Same day check with seconds and milliseconds
const time1 = new Date(2024, 5, 15, 14, 30, 45, 123);
const time2 = new Date(2024, 5, 15, 9, 15, 20, 987);
isSameDay(time1, time2); // Returns: true

Event Filtering

import { isSameDay } from "chronia";

interface Event {
  name: string;
  timestamp: Date;
}

const events: Event[] = [
  { name: "Morning meeting", timestamp: new Date(2024, 5, 15, 9, 0) },
  { name: "Lunch", timestamp: new Date(2024, 5, 15, 12, 30) },
  { name: "Presentation", timestamp: new Date(2024, 5, 16, 14, 0) },
];

const targetDate = new Date(2024, 5, 15);

// Filter events that occurred on target date
const todaysEvents = events.filter((event) =>
  isSameDay(event.timestamp, targetDate),
);
// Returns: [{ name: 'Morning meeting', ... }, { name: 'Lunch', ... }]

Time-Independent Comparisons

import { isSameDay } from "chronia";

// Works with timestamps (numbers)
const date1 = new Date(2024, 5, 15, 10, 30);
const timestamp = date1.getTime();
isSameDay(date1, timestamp); // Returns: true

// Compare user input with current date
function isToday(userDate: Date): boolean {
  return isSameDay(userDate, new Date());
}

const userInput = new Date(2024, 5, 15);
isToday(userInput); // Returns: true if today is June 15, 2024

// Invalid dates return false
isSameDay(new Date("invalid"), new Date(2024, 5, 15)); // Returns: false
isSameDay(new Date(2024, 5, 15), NaN); // Returns: false

Date Range Boundaries

import { isSameDay } from "chronia";

interface DateRange {
  start: Date;
  end: Date;
}

function isOnRangeBoundary(date: Date, range: DateRange): boolean {
  return isSameDay(date, range.start) || isSameDay(date, range.end);
}

const range = {
  start: new Date(2024, 5, 1),
  end: new Date(2024, 5, 30),
};

const checkDate = new Date(2024, 5, 1, 15, 30);
isOnRangeBoundary(checkDate, range); // Returns: true

// Check if date falls on the first day of a range
function isRangeStart(date: Date, range: DateRange): boolean {
  return isSameDay(date, range.start);
}

isRangeStart(checkDate, range); // Returns: true