JavaScript 中獲取當前年份
Harshit Jindal
2021年3月21日
本教程講解如何使用 JavaScript 獲取當前年份。
使用 Date()
物件
Date
是一個內建物件,用於處理所有與日期時間相關的操作。我們可以使用附加的 getFullYear()方法來獲取當前年份。getFullYear()方法根據當地時間返回當前日期的年份。它返回一個介於 1000 和 9999 之間的四位數數字。它返回一個絕對數字。
var currentTime = new Date();
var year = currentTime.getFullYear()
console.log(year);
輸出:
2021
Author: Harshit Jindal
Harshit Jindal has done his Bachelors in Computer Science Engineering(2021) from DTU. He has always been a problem solver and now turned that into his profession. Currently working at M365 Cloud Security team(Torus) on Cloud Security Services and Datacenter Buildout Automation.
LinkedIn