chronia
    Preparing search index...

    Function diffSeconds

    • Calculate the difference in complete seconds between two dates.

      This function calculates the number of complete seconds between two dates by comparing them at the start of each second. Milliseconds are ignored to ensure accurate second 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 complete seconds (negative if dateLeft is before dateRight), or NaN if any input is invalid

      // Basic second difference
      const result = diffSeconds(new Date(2024, 5, 15, 14, 30, 45), new Date(2024, 5, 15, 14, 30, 43));
      // Returns: 2

      // Milliseconds are ignored (same second)
      const result = diffSeconds(new Date(2024, 5, 15, 14, 30, 45, 999), new Date(2024, 5, 15, 14, 30, 45, 0));
      // Returns: 0

      // Works with timestamps
      const timestamp1 = new Date(2024, 5, 15, 14, 31, 0).getTime();
      const timestamp2 = new Date(2024, 5, 15, 14, 30, 30).getTime();
      const result = diffSeconds(timestamp1, timestamp2);
      // Returns: 30

      // Negative result when first date is earlier
      const result = diffSeconds(new Date(2024, 5, 15, 14, 30, 40), new Date(2024, 5, 15, 14, 30, 45));
      // Returns: -5

      // Invalid inputs return NaN
      const result = diffSeconds(new Date("invalid"), new Date(2024, 5, 15, 14, 30, 45));
      // Returns: NaN
      • Compares dates at the start of each second for accurate second counting
      • Milliseconds are ignored
      • Accepts both Date objects and numeric timestamps
      • Returns NaN for: Invalid Date, NaN, Infinity, -Infinity
      • Handles minute, hour, day, month, and year boundaries correctly
      • Uses Math.round to ensure integer results