How to get the current year using JavaScript ?

The purpose of this article is to get the current year by using JavaScript getFullYear() method for some specified date. This method is used to fetch the year from a given Date object.

Syntax:

DateObject.getFullYear() 

Example: It has applications such as getting the current year. It is used in websites to validate the age of the user.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body style="text-align:center;">
    <h1>
        GeeksForGeeks
    </h1>
    <h2>
        How to get the current 
        year in JavaScript?
    </h2>
  
    <button onclick="geeks()">
        click me
    </button>
      
    <h4 id="myID"></h4>
      
    <script>
        function geeks() {
      
            // Creating Date Object 
            var dateobj = new Date();
  
            // Year from above object 
            // is being fetched using getFullYear() 
            var dateObject = dateobj.getFullYear();
  
            // Printing current year 
            document.getElementById("myID").innerHTML
                = "The Current Year is : " + dateObject;
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before Clicking on Button:



  • After clicking on Button:

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.