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

The copyright text is an essential and must-have element in the footer section of the webpage. The year used in the copyright notice text must be updated with the current year. It is a very hectic task to update copyright year text each year. It’s always recommended for developers to use dynamic year in the copyright section so that the year can be changed automatically and updated with 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 to be 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