chronia
    Preparing search index...

    Function addDays

    • Add the specified number of days to the given date.

      This function validates arguments before processing and returns a new Date instance with the specified number of days added. Fractional days are truncated toward zero.

      Parameters

      • date: number | Date

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

      • amount: number

        The number of days to add (can be negative to subtract)

      Returns Date

      A new Date object with the days added, or Invalid Date if any input is invalid

      // Add positive days
      const result = addDays(new Date(2025, 0, 1), 5);
      // Returns: 2025-01-06

      // Subtract days (negative amount)
      const result = addDays(new Date(2025, 0, 10), -3);
      // Returns: 2025-01-07

      // Works with timestamps
      const timestamp = Date.now();
      const result = addDays(timestamp, 7);
      // Returns: Date 7 days from now

      // Fractional amounts are truncated
      const result = addDays(new Date(2025, 0, 1), 1.9);
      // Returns: 2025-01-02 (1.9 truncated to 1)

      // Invalid inputs return Invalid Date
      const result = addDays(new Date("invalid"), 5);
      // Returns: Invalid Date

      const result = addDays(new Date(2025, 0, 1), NaN);
      // Returns: Invalid Date
      • Validates arguments before conversion (consistent with library patterns)
      • Accepts both Date objects and numeric timestamps
      • Fractions are truncated using Math.trunc (1.9 → 1, -1.9 → -1)
      • Returns Invalid Date for: Invalid Date, NaN, Infinity, -Infinity
      • Always returns a new Date instance (does not mutate input)