Get the current year in JavaScript

Total
0
Shares

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:

_x000D_

_x000D_

document.getElementById("year").innerHTML = new Date().getFullYear();

_x000D_

footer {
  text-align: center;
  font-family: sans-serif;
}

_x000D_

<footer>
    ©<span id="year"></span> by Donald Duck
</footer>

_x000D_

_x000D_

_x000D_

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

Source: StackOverflow.com

Leave a Reply

Your email address will not be published. Required fields are marked *