From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/date/getyear/index.html | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getyear/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/date/getyear/index.html') diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getyear/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getyear/index.html new file mode 100644 index 0000000000..29f69b85d5 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getyear/index.html @@ -0,0 +1,114 @@ +--- +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

+ + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial 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

+ + -- cgit v1.2.3-54-g00ecf