弃用
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 兼容性表格 at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

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() 方法。

句法

dateObj.getYear()
					

返回值

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

描述

  • 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() 而不是 getYear() so that the year is specified in full.

Backward compatibility

Behavior in JavaScript 1.2 and earlier

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.

范例

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
					

规范

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

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
getYear 弃用 Chrome 1 Edge 12 Firefox 1 IE 3 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100

图例

完整支持

完整支持

弃用。不要用于新网站。

弃用。不要用于新网站。

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. Date
  3. 方法
    1. Date.UTC()
    2. Date.now()
    3. Date.parse()
    4. Date.prototype.getDate()
    5. Date.prototype.getDay()
    6. Date.prototype.getFullYear()
    7. Date.prototype.getHours()
    8. Date.prototype.getMilliseconds()
    9. Date.prototype.getMinutes()
    10. Date.prototype.getMonth()
    11. Date.prototype.getSeconds()
    12. Date.prototype.getTime()
    13. Date.prototype.getTimezoneOffset()
    14. Date.prototype.getUTCDate()
    15. Date.prototype.getUTCDay()
    16. Date.prototype.getUTCFullYear()
    17. Date.prototype.getUTCHours()
    18. Date.prototype.getUTCMilliseconds()
    19. Date.prototype.getUTCMinutes()
    20. Date.prototype.getUTCMonth()
    21. Date.prototype.getUTCSeconds()
    22. Date.prototype.getYear()
    23. Date.prototype.setDate()
    24. Date.prototype.setFullYear()
    25. Date.prototype.setHours()
    26. Date.prototype.setMilliseconds()
    27. Date.prototype.setMinutes()
    28. Date.prototype.setMonth()
    29. Date.prototype.setSeconds()
    30. Date.prototype.setTime()
    31. Date.prototype.setUTCDate()
    32. Date.prototype.setUTCFullYear()
    33. Date.prototype.setUTCHours()
    34. Date.prototype.setUTCMilliseconds()
    35. Date.prototype.setUTCMinutes()
    36. Date.prototype.setUTCMonth()
    37. Date.prototype.setUTCSeconds()
    38. Date.prototype.setYear()
    39. Date.prototype.toDateString()
    40. Date.prototype.toGMTString()
    41. Date.prototype.toISOString()
    42. Date.prototype.toJSON()
    43. Date.prototype.toLocaleDateString()
    44. Date.prototype.toLocaleString()
    45. Date.prototype.toLocaleTimeString()
    46. Date.prototype.toSource()
    47. Date.prototype.toString()
    48. Date.prototype.toTimeString()
    49. Date.prototype.toUTCString()
    50. Date.prototype.valueOf()
    51. Date.prototype[@@toPrimitive]
  4. 继承:
  5. Function
  6. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  7. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  8. Object
  9. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  10. 方法
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toSource()
    10. Object.prototype.toString()
    11. Object.prototype.valueOf()
    12. Object.setPrototypeOf()