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:
Example
First, we will create a new Date object that will return the current date:
Now, call the “getFullYear()” method to get the current year of the current date:
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:
In the JavaScript file, use the below lines of code for getting the current year with the copyright:
<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:
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.