chronia
    Preparing search index...

    Function diffMilliseconds

    • Calculate the difference in milliseconds between two dates.

      This function calculates the exact difference in milliseconds between two dates. This is the most precise time difference calculation available, equivalent to subtracting the results of getTime().

      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 milliseconds (negative if dateLeft is before dateRight), or NaN if any input is invalid

      // Basic millisecond difference
      const result = diffMilliseconds(new Date(2024, 5, 15, 14, 30, 45, 500), new Date(2024, 5, 15, 14, 30, 45, 100));
      // Returns: 400

      // Exact same time returns 0
      const result = diffMilliseconds(new Date(2024, 5, 15, 14, 30, 45, 123), new Date(2024, 5, 15, 14, 30, 45, 123));
      // Returns: 0

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

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

      // Invalid inputs return NaN
      const result = diffMilliseconds(new Date("invalid"), new Date(2024, 5, 15, 14, 30, 45, 0));
      // Returns: NaN
      • Returns exact millisecond difference (no rounding or truncation)
      • Equivalent to: dateLeft.getTime() - dateRight.getTime()
      • Accepts both Date objects and numeric timestamps
      • Returns NaN for: Invalid Date, NaN, Infinity, -Infinity
      • Most precise time difference function in the library
      • Useful for performance measurements and precise time calculations