`addDays` produces an unexpected date when `amount` is a string
Description
While testing addDays, I noticed that passing a string as the amount parameter results in a seemingly valid but unexpected date instead of either rejecting the input or producing a more obvious result.
Reproduction
import { addDays } from 'date-fns';
const result1 = addDays(new Date(2026, 8, 10), 10);
/* 2026-09-19T18:30:00.000Z */
const result2 = addDays(new Date(2026, 8, 10), '10');
/* 2629-06-05T18:30:00.000Z */
const result3 = addDays(new Date(2026, 8, 10), 1000);
/* 2629-06-05T18:30:00.000Z */
console.log({ result1, result2, result3 });Expected Behavior
Since amount is documented as a number, I expected passing a string to either:
- be rejected (e.g. return
Invalid Dateor otherwise fail predictably), or - behave consistently with the numeric value
10if numeric strings are intentionally supported.
Instead, passing "10" results in a completely different valid date due to JavaScript string concatenation.
Possible cause
It appears the implementation performs arithmetic similar to:
date.setDate(date.getDate() + amount);When amount is a string:
10 + "10" // => "1010"which effectively becomes:
date.setDate(1010);This results in a valid but unintended date.
Question
Is this the intended behavior for unsupported input, or would it make sense for addDays to fail more predictably when amount is not a number?
I suspect the same behavior may also exist in other
add*utilities that accept anamountparameter, as they may follow a similar implementation pattern.
If this is considered unintended behavior, I'd be happy to work on a PR to address it.
Source: date-fns/date-fns