Date.prototype.getUTCFullYear()

Date 实例的 getUTCFullYear() 方法根据世界时间返回该日期的年份。

¥The getUTCFullYear() method of Date instances returns the year for this date according to universal time.

Try it

语法

¥Syntax

js
getUTCFullYear()

参数

¥Parameters

没有任何。

¥None.

返回值

¥Return value

一个整数,表示根据通用时间给定日期的年份。如果日期是 invalid,则返回 NaN

¥An integer representing the year for the given date according to universal time. Returns NaN if the date is invalid.

描述

¥Description

getYear() 不同,getUTCFullYear() 返回的值是绝对数字。对于 1000 年到 9999 年之间的日期,getFullYear() 返回一个四位数字,例如 1995。使用此功能可确保年份符合 2000 年之后的年份。

¥Unlike getYear(), the value returned by getUTCFullYear() is an absolute number. For dates between the years 1000 and 9999, getFullYear() returns a four-digit number, for example, 1995. Use this function to make sure a year is compliant with years after 2000.

示例

¥Examples

使用 getUTCFullYear()

¥Using getUTCFullYear()

以下示例将当前年份的四位值分配给变量 year

¥The following example assigns the four-digit value of the current year to the variable year.

js
const today = new Date();
const year = today.getUTCFullYear();

规范

Specification
ECMAScript Language Specification
# sec-date.prototype.getutcfullyear

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看