All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Get Current Year

Last Updated : Mar 11, 2024

JavaScript Get Current Year

In this article we will show you the solution of JavaScript get current year, the JavaScript method "get the current year" can be used to determine the year currently in use.

Javascript comes has a built-in method like this one.

When utilising the system which the user is now using, this function compares the year in accordance with the time zone in effect at the time.

Using the JavaScript getFullYear() method, which returns the complete year in 4-digit format for a given date, the objective is to determine the current year.

The year can be obtained from a Date object using this function. Now move to the concept of javascript get current year.

Step By Step Guide On JavaScript Get Current Year :-

When dealing with actions involving the date and time, JavaScript has a built-in object called Date.

The Date object can be used to display the time and date that are current for that specific user zone and are local time.

This date is given back as the method's return value.

The year of that date is represented by a four-digit value that is returned by the Date.getFullYear() method.

This value was given back as an absolute number.This method doesn't have any parameters.

<!DOCTYPE html>
<html>
<body style="text-align:center;">
    <h1>Welcome to the Talkerscode</h1>
    <h2>
       Example of Javascript get current year
    </h2>
    <button onclick="example()"> click me </button>
    <h4 id="myID"></h4>
    <script>
    function example() {
        var dateobj = new Date();
        var dateObject = dateobj.getFullYear();
        document.getElementById("myID").innerHTML =
        "The Current Year is " + dateObject;
    }
    </script>
</body>
  </html>
  1. As you can see, we've developed an HTML page on which we'll utilise the javascript method get current year to retrieve the current year based on the user's current date and time.
  2. We begin our code with HTML tags and the DOCTYPE HTML element. The BODY tag, which has the text alignment set to centre, is then used to begin the code body.
  3. Next, we defined two straightforward H1 and H2 tags that will display the texts Welcome to the Talkerscode and Example of Javascript retrieve current year, respectively.
  4. Next, we used the button tag to construct a button that, when clicked, executes an action and displays the text "click me."
  5. After that, the script is launched using the H4 heading tag, which holds the id.
  6. In the script, the function is defined, and thereafter We've made the object Date under this function.
  7. Next, we utilised the javascript method's get current year parameter to determine the year that the date was created.
  8. Using that id and the dateobject, we then print the current year.
  9. Finally, we use the SCRIPT, BODY, and HTML elements to wrap up our code.

Conclusion :-

Thus, we have successfully acquired the knowledge of javascript retrieve current year.

Moreover, we discovered that this technique returns a four-digit absolute number, the year of the date, as the return result.

A JavaScript technique which gets the current year returns a number.

This number denotes the current year at the provided date and time, which is the one that's represented by the year.

I hope this article on JavaScript get current year helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪