setYear

Commons
stable

Return a new Date with the full year replaced by the given value. The original date is never mutated.

API

Signature

Full TypeScript signature sourced from the util file.

typescript

API

Parameters

Input parameters accepted by this util.

date
required
Date
The source date. It is cloned before the year is updated.
year
required
number
The four-digit year to set (e.g. 2025).

API

Return value

The value returned by this util.

A new Date object with the specified year.

Usage

Examples

Ready-to-paste usage snippets.

Change the year of a date

typescript