The setDay function sets the day of the month for a given date, returning a new Date object with the specified day while preserving all other date and time components. It provides input validation and handles edge cases like day overflow and fractional values.
function setDay(date: DateInput, day: number): Date;
| Parameter | Type | Description |
|---|---|---|
date |
DateInput |
The base date as a Date object, numeric timestamp, or ISO 8601 string |
day |
number |
The day of the month to set (1-31, fractions are truncated toward zero) |
| Type | Description |
|---|---|
Date |
A new Date object with the specified day set, or Invalid Date if any input is invalid |
The setDay function creates a new Date instance with the day of the month set to the specified value. It validates all inputs before processing and handles fractional day values by truncating them toward zero using Math.trunc. The function preserves the year, month, and all time components (hours, minutes, seconds, milliseconds) from the original date.
date argument is a valid Date object or finite numeric timestampday argument is a finite number (not NaN, Infinity, or -Infinity)date argument is an Invalid Date object (e.g., new Date('invalid'))date argument is NaN, Infinity, or -Infinityday argument is NaN, Infinity, or -Infinity15.9 → 15, -15.9 → -15)isValidDateOrNumber, isValidNumber) for consistency across the libraryMath.trunc which truncates toward zero, distinct from Math.floor which always rounds downimport { setDay } from "chronia";
// Set to first day of month
const firstDay = setDay(new Date(2025, 0, 15), 1);
// Returns: Date object representing 2025-01-01
// Set to last day of January
const lastDay = setDay(new Date(2025, 0, 15), 31);
// Returns: Date object representing 2025-01-31
// Using timestamp instead of Date object
const fromTimestamp = setDay(1705334400000, 10);
// Returns: Date object with day set to 10
import { setDay } from "chronia";
// Normalize all dates to first of the month
const dates = [
new Date(2025, 0, 5),
new Date(2025, 0, 15),
new Date(2025, 0, 25),
];
const normalized = dates.map((date) => setDay(date, 1));
// Returns: Array of dates all set to January 1, 2025
import { setDay } from "chronia";
// Day overflow causes rollover to next month
const overflow = setDay(new Date(2025, 0, 15), 32);
// Returns: Date object representing 2025-02-01 (January 32 → February 1)
// Day underflow rolls back to previous month
const underflow = setDay(new Date(2025, 0, 15), 0);
// Returns: Date object representing 2024-12-31 (January 0 → December 31, 2024)
// Negative day values
const negative = setDay(new Date(2025, 0, 15), -1);
// Returns: Date object representing 2024-12-30
import { setDay, isValid } from "chronia";
// Fractional day is truncated toward zero
const fractional = setDay(new Date(2025, 0, 15), 15.9);
// Returns: Date object representing 2025-01-15 (not 2025-01-16)
// Negative fractional value
const negativeFractional = setDay(new Date(2025, 0, 15), -15.9);
// Returns: Date object with day set to -15 (rolls back to previous month)
// Invalid date input
const invalidDate = setDay(new Date("invalid"), 15);
// Returns: Invalid Date
// Invalid day input (NaN)
const invalidDay = setDay(new Date(2025, 0, 15), NaN);
// Returns: Invalid Date
// Check validity before using
const result = setDay(new Date(2025, 0, 15), 20);
if (isValid(result)) {
console.log("Day set successfully:", result);
}
import { setDay } from "chronia";
// Set payment due dates to the 15th of each month
function setPaymentDueDate(invoiceDate: Date): Date {
return setDay(invoiceDate, 15);
}
const invoice = new Date(2025, 0, 5);
const dueDate = setPaymentDueDate(invoice);
// Returns: Date object representing 2025-01-15
// Adjust dates based on business rules
function adjustToBusinessDay(date: Date, preferredDay: number): Date {
const adjusted = setDay(date, preferredDay);
// Additional logic could check if it's a weekend and adjust further
return adjusted;
}