JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

How to get the current year for copyright in JavaScript?

To get the current year for copyright, create a Date object by calling the Date() constructor with no arguments. This will create the current date object. After that, call the getFullYear() method to get the current year.

let now = new Date();
console.log(now); //Tue Feb 22 2022 01:52:14
console.log(now.getFullYear()); //2022

The following example shows how you can insert the current year into an HTML element on the web page:

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="UTF-8" />
    <title>Homepage</title>
  </head>
  <body>
    <div id="header">Header content</div>
    <div id="main-section">Main content</div>
    <div id="footer">
      <p id="copyright"></p>
    </div>
    <script>
      document.getElementById("copyright").innerHTML = `© ${new Date().getFullYear()} Mohit Natani`;
    </script>
  </body>
</html>

Output

Get the current year for copyright

Recommended Posts