How do I get the current year in JavaScript?
Advertisement
Answer
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.