Get the current year in JavaScript

Satch3000 picture Satch3000 · May 14, 2011 · Viewed 698k times · Source

How do I get the current year in JavaScript?

Answer

Jason Harwig picture Jason Harwig · May 14, 2011

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>
    &copy; <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>
    &copy; <span id="year">2018</span> by FooBar
</footer>