chronia

isSameHour

Overview

The isSameHour function checks if two dates fall within the same hour, ignoring minutes, seconds, and milliseconds. It provides a precise way to compare dates at the hour level within Chronia’s consistent comparison API.

Signature

function isSameHour(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 in the same hour, false otherwise

Description

The isSameHour function determines whether two dates occur within the same hour by comparing their year, month, day, and hour components. The minute, second, and millisecond components are ignored in the comparison. Internally, it uses the diffHours function and checks if the difference equals zero.

Specification

Returns true when:

Returns false when:

Behavior Notes

Use Cases

Usage Examples

Time-based Grouping

import { isSameHour } from "chronia";

// Group events by hour
interface Event {
  timestamp: Date;
  type: string;
}

function groupEventsByHour(events: Event[]): Map<string, Event[]> {
  const groups = new Map<string, Event[]>();

  for (const event of events) {
    let found = false;
    for (const [key, group] of groups.entries()) {
      if (isSameHour(event.timestamp, group[0].timestamp)) {
        group.push(event);
        found = true;
        break;
      }
    }
    if (!found) {
      const key = event.timestamp.toISOString();
      groups.set(key, [event]);
    }
  }

  return groups;
}

// Same hour, different minutes
isSameHour(new Date(2024, 5, 15, 14, 0, 0), new Date(2024, 5, 15, 14, 59, 59)); // Returns: true

// Different hours
isSameHour(new Date(2024, 5, 15, 14, 59, 59), new Date(2024, 5, 15, 15, 0, 0)); // Returns: false

Temporal Deduplication

import { isSameHour } from "chronia";

interface Submission {
  userId: string;
  timestamp: Date;
  data: unknown;
}

function isDuplicateSubmission(
  newSubmission: Submission,
  recentSubmissions: Submission[],
): boolean {
  return recentSubmissions.some(
    (existing) =>
      existing.userId === newSubmission.userId &&
      isSameHour(existing.timestamp, newSubmission.timestamp),
  );
}

// Check for duplicate submissions
const recent: Submission[] = [
  { userId: "user123", timestamp: new Date(2024, 5, 15, 14, 30), data: {} },
];

const newSub: Submission = {
  userId: "user123",
  timestamp: new Date(2024, 5, 15, 14, 45),
  data: {},
};

isDuplicateSubmission(newSub, recent); // Returns: true (same hour)

Scheduling Validation

import { isSameHour } from "chronia";

interface Appointment {
  id: string;
  startTime: Date;
  duration: number;
}

function hasHourConflict(
  newAppointment: Appointment,
  existingAppointments: Appointment[],
): boolean {
  return existingAppointments.some((existing) =>
    isSameHour(existing.startTime, newAppointment.startTime),
  );
}

// Check for scheduling conflicts
const existing: Appointment[] = [
  { id: "A1", startTime: new Date(2024, 5, 15, 14, 0), duration: 60 },
];

const newAppt: Appointment = {
  id: "A2",
  startTime: new Date(2024, 5, 15, 14, 30),
  duration: 30,
};

hasHourConflict(newAppt, existing); // Returns: true (same hour)

Activity Tracking

import { isSameHour } from "chronia";

interface Activity {
  action: string;
  timestamp: Date;
}

function countActionsInSameHour(
  activities: Activity[],
  referenceTime: Date,
): number {
  return activities.filter((activity) =>
    isSameHour(activity.timestamp, referenceTime),
  ).length;
}

// Track actions in the same hour
const activities: Activity[] = [
  { action: "login", timestamp: new Date(2024, 5, 15, 14, 10) },
  { action: "view", timestamp: new Date(2024, 5, 15, 14, 25) },
  { action: "purchase", timestamp: new Date(2024, 5, 15, 15, 5) },
];

const reference = new Date(2024, 5, 15, 14, 30);
countActionsInSameHour(activities, reference); // Returns: 2

// Invalid dates return false
isSameHour(new Date("invalid"), new Date(2024, 5, 15, 14, 0)); // Returns: false

// Works with timestamps
isSameHour(
  new Date(2024, 5, 15, 14, 30).getTime(),
  new Date(2024, 5, 15, 14, 15).getTime(),
); // Returns: true