Get Current Year in Javascript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

The get the current year in JavaScript is a method that is used to know about the current year. This method is a built-in method in javascript. This method returns the year according to the current local time of that particular system that the user is using now. Suppose the user is from India, then the result will be according to the local time of India which is GMT+5:30. The returned year is in the format of a four-digit number. In this article, we will discuss all the topics related to this method with examples and explanations where ever necessary.

Introduction

A Date is a built-in object present in JavaScript that is used to handle the operations related to the date and time. We can use make use of the Date object for displaying the date and time that corresponds to the current date and local time of that particular user zone. This date is returned as a return value of the method. The Date.getFullYear() method returns a four-digit number that represents the year of that date. This returned value is an absolute number. This method is able to return any year from the dates ranging from 1000 to 9999 years.

  • There are no parameters for this method. And this method returns a four-digit absolute number as the return value, which corresponds to the year of the date.

Syntax

The syntax of the get the current year in JavaScript method is as follows:

  • Return Value: The get the current year in JavaScript method returns a number. This number represents the current year of the given date and local time.

Commonly Used Methods on the Date Object

There are various methods that can be used to get a data object. These methods are called on these objects. Using these methods, we can fetch the current day, month, or year for any particular date object. Now let us see some methods with examples and explanations.

Date.getMonth

The date.getMonth method returns an integer that represents the month that corresponds to the current local date and time. The returned integer ranges from 0 to 11 which represents the months from January to December. the integer value 1 represents the month of January and the integer value 11 represents the month of December.

Date.getDate

The Date.getDate method represents the date of the present month. The represented date corresponds to the current local time. That is if the user is from India then the time will correspond to IST (Indian Standard Time ).

Date.getFullYear()

The Date.getFullYear() method is used for getting the year of a particular date and local time. This date is returned as a return value of the method. The Date.getFullYear() method returns a four-digit number that represents the year of that date. This returned value is an absolute number. This method is able to return any year from the dates ranging from 1000 to 9999 years. Now let us see the example in the next section to get a better understanding.

Example

In the above sections, we have discussed the various aspects of the get the current year in JavaScript method. Now let us implement these concepts and learn more by using the examples. The example is described below with output and explanation.

a. Using getFullYear()

In this example, we have used the get the getFullYear() method to get the current year that corresponds to the current local time.

Output:

  • After running the code you will get to see it on the web page as an output that is shown below. You can see the button click me.

output-getfullyear-function

  • And when the user will click on the button click me, the user will get to see the output which is the current year of the date. This year will correspond to the current local date and time of the user. The output is shown below.

output-getfullyear-function2

Explanation: In the above example, we have created an HTML page under which we will use the get current year in the javascript method to get the current year according to the present date and time of the local time of the user.

First, we have defined a simple H1 tag and an H2 tag that will display two texts that are Example for: and then How to get the current year in JavaScript? respectively. Then we created a button using the button tag that performs an action when clicked, and it shows the text click me on the button. Then in the script tag, we have created the function example(). Under this function, we have created the object Date. We used the get current year in the javascript method to get the current year of the date.

  • The get current year in javascript method is supported by almost all modern browsers. Given below are the name of some popular browsers that supports the get current year in the javascript method.
Browser NameSupported on versions
Mozilla Firefox1.0 and above
Microsoft Edge12.0 and above
Google Chrome1 and above
Safari1 and above
Opera4.0 and above

Transform your passion for coding into a valuable skillset. Join our JavaScript online course and become a proficient web developer.

Conclusion

  • The get the current year in JavaScript is a method that is used to know about the current date.
  • The getFullYear() is an ECMAScript1 (ES1) feature in JavaScript.
  • There are no parameters for this method. And this method returns a four-digit absolute number as the return value, which corresponds to the year of the date.
  • There are various methods that can be used to get a data object such as Date.getMonth, Date.getDate, and Date.getFullYear().
  • The date.getMonth method returns an integer that represents the month that corresponds to the current local date and time.
  • The Date.getDate method represents the date of the present month. The represented date corresponds to the current local time of that particular user.
  • The Date.getFullYear() method is used for getting the year of a particular date and local time.
  • The get current year in javascript method is supported by almost all modern browsers such as Mozilla Firefox, Microsoft Edge, Google Chrome, Safari browser, and Opera Browser.

See Also: