chronia
    Preparing search index...

    Function diffDays

    • Calculate the difference in calendar days between two dates.

      This function calculates the number of full calendar days between two dates by comparing them at midnight. Time components are ignored to ensure accurate calendar day counting.

      Parameters

      • dateLeft: number | Date

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

      • dateRight: number | Date

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

      Returns number

      The difference in calendar days (negative if dateLeft is before dateRight), or NaN if any input is invalid

      // Basic calendar day difference
      const result = diffDays(new Date(2024, 5, 15), new Date(2024, 5, 14));
      // Returns: 1

      // Time components are ignored (same calendar day)
      const result = diffDays(new Date(2024, 5, 15, 23, 59), new Date(2024, 5, 15, 0, 0));
      // Returns: 0

      // Works with timestamps
      const timestamp1 = new Date(2024, 5, 20).getTime();
      const timestamp2 = new Date(2024, 5, 15).getTime();
      const result = diffDays(timestamp1, timestamp2);
      // Returns: 5

      // Negative result when first date is earlier
      const result = diffDays(new Date(2024, 5, 10), new Date(2024, 5, 15));
      // Returns: -5

      // Invalid inputs return NaN
      const result = diffDays(new Date("invalid"), new Date(2024, 5, 15));
      // Returns: NaN
      • Compares dates at midnight for accurate calendar day counting
      • Time components (hours, minutes, seconds, milliseconds) are ignored
      • Accepts both Date objects and numeric timestamps
      • Returns NaN for: Invalid Date, NaN, Infinity, -Infinity
      • Handles leap years, month boundaries, and year boundaries correctly
      • Uses Math.round to ensure integer results