Create a new Date()
object and call 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:
<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>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…