JavaScript

Get the Current Year for Copyright Using JavaScript

On web pages, updating the year in the copyright notice is a tricky task for developers, as it needs to be changed every year. Many people don’t focus to update the copyright year every year. However, with the provided solution in the given article, developers don’t need to change it every year as it will be done automatically.

This article will define the method for getting the current year for copyright on the web page using JavaScript.

How to Get the Current Year for Copyright Using JavaScript?

For getting the current year for copyright, use the JavaScript “getFullYear()” method of the Date Object. It gives the number as output representing the year of the given date.

Syntax

Follow the given syntax for getting the current year:

new Date().getFullYear()

Example

First, we will create a new Date object that will return the current date:

let date = new Date();

Now, call the “getFullYear()” method to get the current year of the current date:

console.log(date.getFullYear());

The given output shows that the current year is “2022”:

In the above part of the example, we have seen how the “getFullYear()” method gives the year. Let’s retrieve the year of the current year for the copyright on the web page. 

To do so, first, create a “div” in the HTML file that will show as a footer on the web page. Assign an id “copyrightYear” and set the background color using the RGB code:

<div id="copyrightYear" style="background-color:rgb(112, 109, 109)"></div>

In the JavaScript file, use the below lines of code for getting the current year with the copyright:

const copyrightFooter = `
 <p>
  Copyright © ${new Date().getFullYear()} Linuxhint
 </p>
`;

In the above code snippet, the variable “copyrightFooter” uses backticks instead of single quotes to create a template string that stores an HTML “p” element, where the <p> tag contains the copyright message with the current year.

Now, display the current year with copyright on the web page using the “innerHTML” property by getting the reference of div using its assigned id with the help of the “getElementById()” method:

document.getElementById('copyrightYear').innerHTML = copyrightFooter;

Output

We have provided the method for getting the current year for copyright using JavaScript.

Conclusion

To get the current year for copyright, use the “Date().getFullYear()” method. The getFullYear() method of the Date object gives the current year of the current date. In this article, we defined the methods for getting the current year for copyright.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.