How to get Current Year in JavaScript

How to get Current Year in JavaScript

current year in Java script

How to get Current Year in JavaScript

How to get Current Year in JavaScript

How to get Current Year in JavaScript

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe

How to get Current Year in JavaScript

Current Year in JavaScript 

In this blog post, we will learn how to get the current year in JavaScript, used for retrieving date and time in web development Applications. You will be able to get the current year for your projects in web development.

JavaScript is a flexible coding language, and also handles retrieving dates and time which is important for most of the applications. This tutorial of date and time will give you examples of how to display the current year on a website, calculate ages, or create an event countdown timer by using different in-built methods in the JavaScript.


Enroll now and take the first step towards a successful career. Click here to join our Web Development courses today!


What is JavaScript?

Javascript is a widely used programming language for Web Development.  It is also called as client-side scripting language as it runs on the browsers, not on the server. It helps us to develop interactive and dynamic web applications. Javascript also enables us to add dynamic functionalities like user interactions, adding or changing the content of a page without refreshing.

The Date Object

Firstly, we will go through the basics of Javascript. As javascript has a built-in object called Date which is used to retrieve the date and time information.

To display the Date and time we create the Date Object by calling the Date Constructor without specifying any arguments

Code –

Let CurrentDate= new Date();


Also Read: How to Become a Freelance Web Developer


Retrieving the Current Year

In JavaScript, we use the current year method which retrieves the data of the year.  As this method is a built-in method in javascript. When we use the current year method, it will return the year in the four digit number format. Here we explained the methods related to date and time with examples.

  1.     Date.getFullYear()

Syntax- DateObject.getFullYear()

Code- 

let currentDate = new Date();

let currentYear = currentDate.getFullYear();

console.log(currentYear);

Output-

current year
current year

Explanation-

In the above example, we have first created a Date Object i.e currentDate and then we have called getFullYear() method by using the date object which is assigned to the new variable currentYear. Then to print the output of the year on the browsers console, we use the console.log() function.


Also Read: HTML Web Pages Can Be Read And Rendered By


Using the Current Year Method

To get the current year,we will be using the getFullYear() method. Here by giving an example we will explain how we can retrieve the year by using getFullYear() method.

Code-

<!DOCTYPE html>

<html>

<body style=”text-align:center;”>

    <h1>Example on Retrieving Current Year:</h1>

    <h2>

        How to get the current 

        year in JavaScript?

    </h2>

    <button onclick=”demo()”> click me </button>

    <h4 id=”myID”></h4>

    <script>

    function demo() {

        

        // Creating Date Object 

        var dateobj = new Date();

        

        // Year from the 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>

Output-

current-year-output

Explanation- 

In the given example, we will be using the current year method in the html page. 

This html page will help us to get the year by using the getFullYear method.

Here we had used H1 and H2 Tags for displaying the title. Then we created a button Which displays a text Click me. When the user clicks on the button, it will perform the action and display the output in the form of the year.

 Conclusion

In this Blog, we learned how to retrieve the year by using Javascript and its getFullYear method. This information of retrieving date and time is valuable while developing different web Applications. You can use this information easily while using date and time functions. It will be more helpful to build your utilities perfectly in the web project.

 Technogeeks provides the best guidance to grow your web Development skills in the route of your choice. If you are looking for Full Stack Web Development with React in Pune; Check out our course page below and get ready to dive into the world of web development!

Contact Us For FREE Career Counseling +91 8600998107/ +91 7028710777 

Prince

Prince

Leave a Reply

Your email address will not be published. Required fields are marked *

Blogs You May Like

Get in touch to claim Best Available Discounts.

If You Are Looking for Job Assistance Please Fill Up the Form.

× How can I help you?