Date.prototype.setYear()
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see thecompatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
ThesetYear() method ofDate instances sets the year for a specified date according to local time.
However, the way the legacysetYear() method sets year values is different from how the preferredsetFullYear() method sets year values — and in some cases, also different from hownew Date() andDate.parse() set year values. Specifically, given two-digit numbers, such as22 and61:
setYear()interprets any two-digit number as an offset to1900; sodate.setYear(22)results in the year value being set to1922, anddate.setYear(61)results in the year value being set to1961. (In contrast, whilenew Date(61, 1)also results in the year value being set to1961,new Date("2/1/22")results in the year value being set to2022; and similarly forDate.parse()).setFullYear()does no special interpretation but instead uses the literal two-digit value as-is to set the year; sodate.setFullYear(61)results in the year value being set to0061, anddate.setFullYear(22)results in the year value being set to0022.
Because of those differences in behavior, you should no longer use the legacysetYear() method, but should instead use the preferredsetFullYear() method.
In this article
Syntax
setYear(yearValue)Parameters
yearValueAn integer.
Return value
Changes theDate object in place, and returns its newtimestamp. IfyearValue isNaN (or other values that getcoerced toNaN, such asundefined), the date is set toInvalid Date andNaN is returned.
Description
IfyearValue is a number between 0 and 99 (inclusive), then the year fordateObj is set to1900 + yearValue. Otherwise, the year fordateObj is set toyearValue.
Examples
>Using setYear()
The first two lines set the year to 1996. The third sets the year to 2000.
const theBigDay = new Date();theBigDay.setYear(96);theBigDay.setYear(1996);theBigDay.setYear(2000);Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-date.prototype.setyear> |