chronia
    Preparing search index...

    Function isEqual

    • Check if two dates are equal.

      This function compares two dates and returns true if they represent the same point in time. The comparison can be performed at different granularities (year, month, day, hour, minute, second, or millisecond).

      Parameters

      • a: number | Date

        The first date as a Date object or timestamp (number)

      • b: number | Date

        The second date as a Date object or timestamp (number)

      • Optionalopts: { unit?: TimeUnit }

        Optional configuration object

        • Optionalunit?: TimeUnit

          The unit of comparison (year, month, day, hour, minute, second, millisecond). Defaults to "millisecond"

      Returns boolean

      True if date a is equal to date b, false otherwise or if either date is invalid

      // Basic comparison (millisecond precision)
      const date1 = new Date(2025, 0, 1, 12, 0, 0);
      const date2 = new Date(2025, 0, 1, 12, 0, 0);
      const result = isEqual(date1, date2);
      // Returns: true

      // Compare at day granularity
      const result2 = isEqual(
      new Date(2025, 0, 1, 9, 0),
      new Date(2025, 0, 1, 17, 0),
      { unit: "day" }
      );
      // Returns: true (same day, different times)

      // Works with timestamps
      const timestamp = Date.now();
      const result3 = isEqual(timestamp, new Date(timestamp));
      // Returns: true

      // Different dates return false
      const result4 = isEqual(new Date(2025, 0, 1), new Date(2025, 0, 2));
      // Returns: false

      // Invalid dates return false
      const result5 = isEqual(new Date("invalid"), new Date(2025, 0, 1));
      // Returns: false
      • Validates arguments before processing (consistent with library patterns)
      • Returns false for any invalid input (Invalid Date, NaN, Infinity, -Infinity)
      • Accepts both Date objects and numeric timestamps
      • When using unit-based comparison, dates are truncated to the specified unit before comparing
      • Unit comparison example: comparing by "day" ignores hours, minutes, seconds, and milliseconds
      • Two dates with the same timestamp are always equal (at millisecond precision)