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