Skip to content

JavaScript get current year

  • by

To get the current year in JavaScript, you can use the built-in Date object and its getFullYear() method. To get the current year in JavaScript, you can use the following syntax:

const now = new Date();
const currentYear = now.getFullYear();

JavaScript gets current year example

Simple example code

<!DOCTYPE html>
<html>
  <head>
    <title>My Web Page</title>
  </head>
  <body>
    <h1>Welcome to my web page!</h1>
    <p>This page was last updated in <span id="current-year"></span>.</p>

    <script>
      const now = new Date();
      const currentYear = now.getFullYear();
      const currentYearElement = document.getElementById("current-year");
      currentYearElement.textContent = currentYear;
    </script>
  </body>
</html>

Output:

JavaScript get current year

When you load this page in a web browser, the text in the paragraph will be updated to show the current year.

Comment if you have any doubts or suggestions on this Js date object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Tags:

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading