試一試
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 |
瀏覽器相容性
載入中…