Search code examples
javascriptdate

Get the current year in JavaScript


How do I get the current year in JavaScript?


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 Stack Overflow
    </footer>

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