File size: 2,744 Bytes
780c9fe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
---
title: Date.prototype.getYear()
short-title: getYear()
slug: Web/JavaScript/Reference/Global_Objects/Date/getYear
page-type: javascript-instance-method
status:
- deprecated
browser-compat: javascript.builtins.Date.getYear
sidebar: jsref
---
{{Deprecated_Header}}
The **`getYear()`** method of {{jsxref("Date")}} instances returns the year for this date according to local time. Because `getYear()` does not return full years ("year 2000 problem"), it is deprecated and has been replaced by the {{jsxref("Date/getFullYear", "getFullYear()")}} method.
## Syntax
```js-nolint
getYear()
```
### Parameters
None.
### Return value
An integer representing the year for the given date according to local time, minus 1900. Returns `NaN` if the date is [invalid](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_epoch_timestamps_and_invalid_date).
- For years greater than or equal to 2000, the value is 100 or greater. For example, if the year is 2026, `getYear()` returns 126.
- For years between and including 1900 and 1999, the value returned by `getYear()` is between 0 and 99. For example, if the year is 1976, `getYear()` returns 76.
- For years less than 1900, the value returned by `getYear()` is less than 0. For example, if the year is 1800, `getYear()` returns -100.
This method essentially returns the value of {{jsxref("Date/getFullYear", "getFullYear()")}} minus 1900. You should use `getFullYear()` instead, so that the year is specified in full.
## Examples
### Years between 1900 and 1999
The second statement assigns the value 95 to the variable `year`.
```js
const xmas = new Date("1995-12-25");
const year = xmas.getYear(); // returns 95
```
### Years above 1999
The second statement assigns the value 100 to the variable `year`.
```js
const xmas = new Date("2000-12-25");
const year = xmas.getYear(); // returns 100
```
### Years below 1900
The second statement assigns the value -100 to the variable `year`.
```js
const xmas = new Date("1800-12-25");
const year = xmas.getYear(); // returns -100
```
### Setting and getting a year between 1900 and 1999
The third statement assigns the value 95 to the variable `year`, representing the year 1995.
```js
const xmas = new Date("2015-12-25");
xmas.setYear(95);
const year = xmas.getYear(); // returns 95
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Polyfill of `Date.prototype.getYear` in `core-js`](https://github.com/zloirock/core-js#ecmascript-date)
- [es-shims polyfill of `Date.prototype.getYear`](https://www.npmjs.com/package/date.prototype.getyear)
- {{jsxref("Date.prototype.getFullYear()")}}
- {{jsxref("Date.prototype.getUTCFullYear()")}}
- {{jsxref("Date.prototype.setYear()")}}
|