Back

dateObj.getYear()

dateObj.getYear()

The getYear() method returns the year in the specified date according to local time. Because getYear() does not return full years ("year 2000 problem"), it is no longer used and has been replaced by the getFullYear() method.

Syntax

dateObj.getYear()

Parameters

None.

Returns value

The getYear() method returns the year minus 1900; thus:

To take into account years before and after 2000, you should use getFullYear() instead of getYear() so that the year is specified in full.

Backward compatibility

Behavior in JavaScript 1.2 and earlier

The getYear() method returns either a 2-digit or 4-digit year:

Examples

Years between 1900 and 1999

The second statement assigns the value 95 to the variable year.

var Xmas = new Date('December 25, 1995 23:15:00');
var year = Xmas.getYear(); // returns 95

Years above 1999

The second statement assigns the value 100 to the variable year.

var Xmas = new Date('December 25, 2000 23:15:00');
var year = Xmas.getYear(); // returns 100

Years below 1900

The second statement assigns the value -100 to the variable year.

var Xmas = new Date('December 25, 1800 23:15:00');
var 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.

var Xmas = new Date('December 25, 2015 23:15:00');
Xmas.setYear(95);
var year = Xmas.getYear(); // returns 95

  Created by Mozilla Contributors, license: CC-BY-SA 2.5