getFullYear()
方法根据本地时间返回指定日期的年份。
此方法替代 getYear()
。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
语法
dateObj.getFullYear()
返回值
根据当地时间,返回一个对应于给定日期的年份数字。
描述
getFullYear()
返回的值是绝对数。 对于1000到9999之间的日期,getFullYear()
返回一个四位数字,如1995。使用此函数确保在2000年后兼容。
例子
使用getFullYear()
下面的例子将当前年份的四位数值分配给变量year
。
var today = new Date();
var year = today.getFullYear();
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.3. |
ECMAScript 5.1 (ECMA-262) Date.prototype.getFullYear |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) Date.prototype.getFullYear |
Standard | |
ECMAScript (ECMA-262) Date.prototype.getFullYear |
Living Standard |
浏览器兼容性
BCD tables only load in the browser
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-dataand send us a pull request.