Javascript – Get the current year in JavaScript

datejavascript

How do I get the current year in JavaScript?

Best Solution

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>

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