The base date as a Date object or timestamp (number)
The month to set (0-indexed: 0 = January, 11 = December)
A new Date object with the month set, or Invalid Date if any input is invalid
// Set month to June
const result = setMonth(new Date(2025, 0, 15), 5);
// Returns: 2025-06-15
// Set month to January
const result2 = setMonth(new Date(2025, 5, 15), 0);
// Returns: 2025-01-15
// Day overflow adjustment (Jan 31 → Feb 28)
const result3 = setMonth(new Date(2025, 0, 31), 1);
// Returns: 2025-02-28 (non-leap year)
// Fractional month is truncated
const result4 = setMonth(new Date(2025, 0, 15), 5.9);
// Returns: 2025-06-15
// Invalid date returns Invalid Date
const result5 = setMonth(new Date("invalid"), 5);
// Returns: Invalid Date
Set the month of the given date.
This function validates arguments before processing and returns a new Date instance with the specified month set. Fractional months are truncated toward zero.