--- title: Date.prototype.getYear() slug: Web/JavaScript/Reference/Global_Objects/Date/getYear translation_of: Web/JavaScript/Reference/Global_Objects/Date/getYear ---
getYear() 方法返回指定的本地日期的年份。因为 getYear
不返回千禧年[full years] ("year 2000 problem"),所以这个方法不再被使用,现在替换为 {{jsxref("Date.getFullYear", "getFullYear")}} .
dateObj.getYear()
None.
The getYear
method returns the year minus 1900; thus:
getYear
is 100 or greater. For example, if the year is 2026, getYear
returns 126.getYear
is between 0 and 99. For example, if the year is 1976, getYear
returns 76.getYear
is less than 0. For example, if the year is 1800, getYear
returns -100.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.
The getYear
method returns either a 2-digit or 4-digit year:
getYear
is the year minus 1900. For example, if the year is 1976, the value returned is 76.getYear
is the four-digit year. For example, if the year is 1856, the value returned is 1856. If the year is 2026, the value returned is 2026.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
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
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
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
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. |
{{Compat("javascript.builtins.Date.getYear")}}