--- title: Date.prototype.getYear() slug: Web/JavaScript/Reference/Global_Objects/Date/getYear translation_of: Web/JavaScript/Reference/Global_Objects/Date/getYear ---
{{JSRef("Global_Objects", "Date")}} {{Deprecated_header("")}}

getYear() 方法返回指定的本地日期的年份。因为 getYear 不返回千禧年[full years] ("year 2000 problem"),所以这个方法不再被使用,现在替换为 {{jsxref("Date.getFullYear", "getFullYear")}} .

Syntax

dateObj.getYear() 

Parameters

None.

Returns

The getYear method returns the year minus 1900; thus:

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

Backward Compatibility

Behaviour in JavaScript 1.2 and earlier

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

Examples

Example: 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

Example: 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

Example: 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

Example: Setting and getting a year between 1900 and 1999

The second statement assigns the value 95 to the variable year, representing the year 1995.

var Xmas.setYear(95);
var year = Xmas.getYear(); // returns 95

Specifications

Specification Status Comment
ECMAScript 1st Edition. Implemented in JavaScript 1.0 Standard Initial definition.
{{SpecName('ES5.1', '#sec-B.2.4', 'Date.prototype.getYear')}} {{Spec2('ES5.1')}} Defined in the (informative) compatibility annex.
{{SpecName('ES6', '#sec-date.prototype.getyear', 'Date.prototype.getYear')}} {{Spec2('ES6')}} Defined in the (normative) annex for additional features for web browsers.

Browser compatibility

{{Compat("javascript.builtins.Date.getYear")}}

See also