How to Get the Current Year to Display Dynamic Year in Copyright using JavaScript

Copyright text is essential and must be included in the webpage footer. The year used in the copyright notice text must be updated to the current year. It is a very hectic task to update the copyright year text each year. Developers should use a dynamic year in the copyright section so it updates automatically to the current year.

You can get the current year and add the dynamic year in HTML using JavaScript. In this example code, we will show you how to get the current year in JavaScript and change the year dynamically in Copyright text using JavaScript.

Define an HTML element where the dynamic year will be attached and updated with the current year:

<footer>
    <p>© Copyrights <span id="year"></span> by CodexWorld. All rights reserved.</p>
</footer>

Create a Date object and call the getFullYear() method to get the current year in JavaScript.

  • Set the innerHTML to the HTML element (year), it will print the current year in the copyright text section and update each year automatically.
<script>
document.getElementById("year").innerHTML = new Date().getFullYear();
</script>

Leave a reply

keyboard_double_arrow_up