The setMonth function sets the month of a given date and returns a new Date instance with the specified month. It provides robust validation and handles edge cases like day overflow when the target month has fewer days than the original date.
function setMonth(date: DateInput, month: number): Date;
| Parameter | Type | Description |
|---|---|---|
date |
DateInput |
The base date as a Date object, numeric timestamp, or ISO 8601 string |
month |
number |
The month to set (0-indexed: 0 = January, 11 = December) |
| Type | Description |
|---|---|
Date |
A new Date object with the specified month set, or Invalid Date if any input is invalid |
The setMonth function creates a new Date instance with the month component modified to the specified value while preserving all other date and time components (year, day, hours, minutes, seconds, milliseconds). It validates both arguments before processing and intelligently handles day overflow scenarios.
date argument is a valid Date object (not Invalid Date)date argument is a finite numeric timestamp, including:
0, representing January 1, 1970, 00:00:00 UTC)month argument is a finite number (including negative values and values >= 12)Math.truncdate argument is an Invalid Date object (e.g., new Date('invalid'))date argument is NaN, Infinity, or -Infinitymonth argument is NaN, Infinity, or -InfinityMath.trunc (e.g., 5.9 becomes 5, -1.9 becomes -1)13 becomes January of the next year, -1 becomes December of the previous year)import { setMonth } from "chronia";
// Set month to June (month index 5)
const result = setMonth(new Date(2025, 0, 15), 5);
// Returns: Date representing 2025-06-15
// Set month to January (month index 0)
const result2 = setMonth(new Date(2025, 5, 15), 0);
// Returns: Date representing 2025-01-15
// Using timestamp input
const result3 = setMonth(1704067200000, 5); // Jan 1, 2024
// Returns: Date representing 2024-06-01
import { setMonth } from "chronia";
// Generate last day of each month for the year
const baseDate = new Date(2025, 0, 31); // January 31, 2025
const february = setMonth(baseDate, 1);
// Returns: 2025-02-28 (automatically adjusts to last day of February)
const april = setMonth(baseDate, 3);
// Returns: 2025-04-30 (automatically adjusts to last day of April)
const june = setMonth(baseDate, 5);
// Returns: 2025-06-30 (automatically adjusts to last day of June)
// Leap year handling
const leapYear = new Date(2024, 0, 31);
const februaryLeap = setMonth(leapYear, 1);
// Returns: 2024-02-29 (leap year, so February has 29 days)
import { setMonth } from "chronia";
// Move forward by N months
function addMonths(date: Date, monthsToAdd: number): Date {
const currentMonth = date.getMonth();
return setMonth(date, currentMonth + monthsToAdd);
}
const startDate = new Date(2025, 0, 15); // January 15, 2025
const threeMonthsLater = addMonths(startDate, 3);
// Returns: 2025-04-15
// Move backward by N months
const threeMonthsEarlier = addMonths(startDate, -3);
// Returns: 2024-10-15 (previous year)
import { setMonth } from "chronia";
// Handle fractional values
const result = setMonth(new Date(2025, 0, 15), 5.9);
// Returns: 2025-06-15 (5.9 truncated to 5)
const result2 = setMonth(new Date(2025, 0, 15), -1.2);
// Returns: 2024-12-15 (-1.2 truncated to -1, which is December of previous year)
// Handle invalid inputs
const invalid1 = setMonth(new Date("invalid"), 5);
// Returns: Invalid Date
const invalid2 = setMonth(new Date(2025, 0, 15), NaN);
// Returns: Invalid Date
const invalid3 = setMonth(new Date(2025, 0, 15), Infinity);
// Returns: Invalid Date
import { setMonth } from "chronia";
// Calendar navigation with day preservation
function navigateMonth(currentDate: Date, direction: "prev" | "next"): Date {
const currentMonth = currentDate.getMonth();
const newMonth = direction === "next" ? currentMonth + 1 : currentMonth - 1;
return setMonth(currentDate, newMonth);
}
// User viewing January 31, 2025
const current = new Date(2025, 0, 31);
// Navigate to next month
const nextMonth = navigateMonth(current, "next");
// Returns: 2025-02-28 (gracefully handles day overflow)
// Navigate to previous month
const prevMonth = navigateMonth(current, "prev");
// Returns: 2024-12-31 (previous year, December has 31 days)
// Preserves time components
const withTime = new Date(2025, 0, 15, 14, 30, 45, 123);
const movedMonth = setMonth(withTime, 5);
// Returns: 2025-06-15 14:30:45.123 (time preserved)