Create a new Date()
object and call getFullYear()
:
(创建一个new Date()
对象并调用getFullYear()
:)
new Date().getFullYear()
// returns the current year
Hijacking the accepted answer to provide some basic example context like a footer that always shows the current year:
(劫持已接受的答案以提供一些基本示例上下文,例如始终显示当前年份的页脚:)
<footer>
© <span id="year"></span>
</footer>
Somewhere else executed after the HTML above has been loaded:
(加载以上HTML之后执行的其他地方:)
<script>
document.getElementById("year").innerHTML = new Date().getFullYear();
</script>
document.getElementById("year").innerHTML = new Date().getFullYear();
footer { text-align: center; font-family: sans-serif; }
<footer> © <span id="year">2018</span> by FooBar </footer>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…