Date.prototype.getYear() - JavaScript 编辑

Deprecated

This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

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()

Return value

A number representing the year of the given date, according to local time, minus 1900.

Description

  • For years greater than or equal to 2000, the value returned by getYear() 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.

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:

  • For years between and including 1900 and 1999, the value returned by getYear() is the year minus 1900. For example, if the year is 1976, the value returned is 76.
  • For years less than 1900 or greater than 1999, the value returned by 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.

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

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'Date.prototype.getYear' in that specification.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:83 次

字数:5952

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文