The setYear function sets the year component of a given date to a specified value and returns a new Date instance. It validates inputs, handles edge cases like leap year adjustments, and ensures immutability by not modifying the original date.
function setYear(date: DateInput, year: number): Date;
| Parameter | Type | Description |
|---|---|---|
date |
DateInput |
The base date as a Date object, numeric timestamp, or ISO 8601 string |
year |
number |
The year to set (can be negative for BC dates, fractional values are truncated toward zero) |
| Type | Description |
|---|---|
Date |
A new Date object with the specified year set, or Invalid Date if any input is invalid |
The setYear function creates a new Date object with the year component set to the specified value while preserving all other date and time components (month, day, hours, minutes, seconds, milliseconds). It provides robust input validation and handles edge cases such as leap year adjustments.
date argument is a valid Date object (not Invalid Date)date argument is a finite numeric timestamp (positive, zero, or negative)year argument is a finite number (positive, zero, or negative)date argument is an Invalid Date object (e.g., new Date('invalid'))date argument is NaN, Infinity, or -Infinityyear argument is NaN, Infinity, or -InfinityMath.trunc, which rounds toward zero (2023.9 → 2023, -2023.9 → -2023)date parameterimport { setYear } from "chronia";
// Set year to future year
const original = new Date(2025, 0, 15, 10, 30, 0);
const future = setYear(original, 2030);
// Returns: 2030-01-15 10:30:00 (same month, day, time)
// Set year to past year
const past = setYear(new Date(2025, 0, 15), 2020);
// Returns: 2020-01-15
// Works with timestamps too
const timestamp = new Date(2025, 5, 10).getTime();
const changed = setYear(timestamp, 2028);
// Returns: 2028-06-10
import { setYear } from "chronia";
// Leap year to non-leap year (Feb 29 → Feb 28)
const leapDay = new Date(2020, 1, 29); // Feb 29, 2020
const nonLeapYear = setYear(leapDay, 2021);
// Returns: 2021-02-28 (automatically adjusted)
// Non-leap year to leap year (Feb 28 → Feb 28)
const regularDay = new Date(2021, 1, 28);
const toLeapYear = setYear(regularDay, 2020);
// Returns: 2020-02-28 (no adjustment needed)
// Leap year to leap year (Feb 29 → Feb 29)
const leapToLeap = setYear(new Date(2020, 1, 29), 2024);
// Returns: 2024-02-29 (both are leap years)
import { setYear, isValid } from "chronia";
// Fractional years are truncated
const truncated = setYear(new Date(2025, 0, 15), 2023.9);
// Returns: 2023-01-15 (2023.9 → 2023)
// Negative years for BC dates
const ancient = setYear(new Date(2025, 0, 15), -100);
// Returns: Date representing year -100 (100 BC)
// Invalid date input returns Invalid Date
const invalidDate = setYear(new Date("invalid"), 2025);
console.log(isValid(invalidDate)); // false
// Invalid year returns Invalid Date
const invalidYear = setYear(new Date(2025, 0, 15), NaN);
console.log(isValid(invalidYear)); // false
import { setYear } from "chronia";
// Project an event to next year
function projectToNextYear(eventDate: Date): Date {
const currentYear = eventDate.getFullYear();
return setYear(eventDate, currentYear + 1);
}
const event = new Date(2025, 5, 15, 14, 0, 0);
const nextYearEvent = projectToNextYear(event);
// Returns: 2026-06-15 14:00:00
// Calculate historical equivalent
function historicalEquivalent(modernDate: Date, historicalYear: number): Date {
return setYear(modernDate, historicalYear);
}
const today = new Date(2025, 10, 22);
const in1990 = historicalEquivalent(today, 1990);
// Returns: 1990-11-22 (same month and day, different year)
import { setYear } from "chronia";
// Calculate when a birthday occurs in a specific year
function birthdayInYear(birthDate: Date, targetYear: number): Date {
return setYear(birthDate, targetYear);
}
// Regular birthday
const birthday = new Date(1990, 5, 15);
const birthday2025 = birthdayInYear(birthday, 2025);
// Returns: 2025-06-15
// Leap year birthday edge case
const leapBirthday = new Date(2000, 1, 29); // Feb 29, 2000
const in2025 = birthdayInYear(leapBirthday, 2025);
// Returns: 2025-02-28 (automatically adjusted for non-leap year)
const in2024 = birthdayInYear(leapBirthday, 2024);
// Returns: 2024-02-29 (leap year preserved)