Date.prototype.getFullYear()

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

getFullYear() 方法用於獲取 Date 例項的年份,根據本地時間。

請使用此方法而不是 getYear() 方法。

試一試

const moonLanding = new Date("July 20, 69 00:20:18");

console.log(moonLanding.getFullYear());
// Expected output: 1969

語法

js
getFullYear()

引數

無。

返回值

一個整數,表示給定日期的年份(根據本地時間)。如果日期 無效,則返回 NaN

描述

getYear() 不同,getFullYear() 返回的值是絕對數字。對於 1000 年到 9999 年之間的日期,getFullYear() 返回一個四位數,例如 1995。使用此函式可確保年份符合 2000 年之後的年份。

示例

使用 getFullYear()

變數 fullYear 的值為 1995,基於 Date 物件 xmas95 的值。

js
const xmas95 = new Date("1995-12-25T23:15:30");
const fullYear = xmas95.getFullYear();

console.log(fullYear); // 1995

規範

規範
ECMAScript® 2026 語言規範
# sec-date.prototype.getfullyear

瀏覽器相容性

另見