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