chronia
    Preparing search index...

    Function isBeforeOrEqual

    • Check if the first date is before or equal to the second date.

      This function compares two dates and returns true if the first date is chronologically before or equal to the second date. 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 before or equal to date b, false otherwise or if either date is invalid

      // Basic comparison (millisecond precision)
      const result = isBeforeOrEqual(new Date(2025, 0, 1), new Date(2025, 0, 2));
      // Returns: true

      // Equality returns true
      const date = new Date(2025, 0, 1);
      const result2 = isBeforeOrEqual(date, date);
      // Returns: true

      // Compare at day granularity
      const result3 = isBeforeOrEqual(
      new Date(2025, 0, 1, 0, 0),
      new Date(2025, 0, 1, 23, 59),
      { unit: "day" }
      );
      // Returns: true (same day)

      // Works with timestamps
      const result4 = isBeforeOrEqual(Date.now() - 1000, Date.now());
      // Returns: true (1 second ago is before current time)

      // Invalid dates return false
      const result5 = isBeforeOrEqual(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
      • Includes equality in the comparison (a <= b)
      • 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