variety of purposes, such as displaying the current year in the copyright notice of your website or for
performing calculations involving the current date. With just a few lines of code, you’ll be able to display
the current year on your website with ease.
Getting the Current Year
contain the current date and time by default. The Date object provides a method called
getFullYear(), which returns the four-digit year value of the date.
const currentDate = new Date(); const currentYear = currentDate.getFullYear(); console.log(currentYear);
In this example, we first create a new Date object called currentDate. We
then use the getFullYear() method to get the current year, which we store in the variable
currentYear. Finally, we log the current year to the console.
Displaying the Current Year on Your Website
your website. For example, you could update the copyright notice in your website’s footer to include the
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Current Year Example</title> <footer> © <span id="currentYear"></span> Your Website </footer> <script src="script.js"></script>
const currentDate = new Date(); const currentYear = currentDate.getFullYear(); document.getElementById('currentYear').textContent = currentYear;
In this example, we first create an HTML file with a footer containing a span element with
the ID currentYear. This is where the current year will be displayed. Then, we create a
year, and then we update the textContent of the span element with the ID
currentYear to display the current year on the page.
By using the Date object and its getFullYear() method, you can easily display
the current year on your website or use it for other purposes in your application. Happy coding!