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 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.

O método setYear() atribui o ano para a data especificada de acordo com o horário local. Devido setYear() não colocar o anos cheios ("problema do ano 2000"), ele não é mais utilizado e foi substituído pelo método setFullYear().

Sintaxe

dateObj.setYear(yearValue)

Parâmetros

yearValue

Um inteiro.

Valor de retorno

O número de milissegundos entre 1 de Janeiro de 1970 00:00:00 UTC e a data atualizada.

Descrição

Se yearValue é um número entre 0 e 99 (inclusivo), então o ano para dateObj é atribuido para 1900 + yearValue. Caso contrário, o ano para dateObj é atribuido para yearValue.

Exemplos

Usando setYear()

As duas primeiras linhas atribuem o ano para 1996. O terceiro atribui o ano para 2000.

js
var theBigDay = new Date();

theBigDay.setYear(96);
theBigDay.setYear(1996);
theBigDay.setYear(2000);

Especificações

Specification
ECMAScript® 2025 Language Specification
# sec-date.prototype.setyear

Compatibilidade com navegadores

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
setYear
Deprecated

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Deprecated. Not for use in new websites.

Veja também