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 the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
The setYear() method of Date instances sets the year for a specified date according to local time.
However, the way the legacy setYear() method sets year values is different from how the preferred setFullYear() method sets year values — and in some cases, also different from how new Date() and Date.parse() set year values. Specifically, given two-digit numbers, such as 22 and 61:
- setYear()interprets any two-digit number as an offset to- 1900; so- date.setYear(22)results in the year value being set to- 1922, and- date.setYear(61)results in the year value being set to- 1961. (In contrast, while- new Date(61, 1)also results in the year value being set to- 1961,- new Date("2/1/22")results in the year value being set to- 2022; and similarly for- Date.parse()).
- setFullYear()does no special interpretation but instead uses the literal two-digit value as-is to set the year; so- date.setFullYear(61)results in the year value being set to- 0061, and- date.setFullYear(22)results in the year value being set to- 0022.
Because of those differences in behavior, you should no longer use the legacy setYear() method, but should instead use the preferred setFullYear() method.