在 JavaScript 中获取当前年份 [英] Get the current year in JavaScript

查看:24
本文介绍了在 JavaScript 中获取当前年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 JavaScript 中获取当前年份?

How do I get the current year in JavaScript?

推荐答案

创建一个 new Date() 对象并调用 getFullYear():

Create a new Date() object and call getFullYear():

new Date().getFullYear()  // returns the current year


示例用法:始终显示当前年份的页脚:


Example usage: a page footer that always shows the current year:

document.getElementById("year").innerHTML = new Date().getFullYear();

footer {
  text-align: center;
  font-family: sans-serif;
}

<footer>
    ©<span id="year"></span> by Donald Duck
</footer>

另见 Date() 构造函数的完整的方法列表.

See also, the Date() constructor's full list of methods.

这篇关于在 JavaScript 中获取当前年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆