The Wayback Machine - https://web.archive.org/web/20240801094208/https://www.geeksforgeeks.org/javascript-date-getyear-method/
Open In App

JavaScript Date getYear() Method

Last Updated : 12 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

JavaScript date.getYear() is used to get the year on a specified date according to universal time. The year we get from the method getYear() is the current year minus 1900.

Syntax:

date.getYear();

Parameters:

  • This function does not accept any parameters.

Return Value:

  • This method returns a value that is subtracted from 1900 to the current year.

Example 1: This example returns the year of the specified date according to universal time.

JavaScript
// "date" is object of Date() class
let date = new Date();

// Use "getYear()" method and stores 
// the returned value to "n"
let n = date.getYear();

// Display the result
console.log("The value returned by getYear() method: " + n)

Output:

The value returned by getYear() method: 119

Example 2: This example only sends the year to the constructor.

JavaScript
// Year 2000 is assign to the 
// constructor of Date() class
let date = new Date('2000')
  
// Use "getYear()" method and stores
// the returned value to "n"
let n = date.getYear();
  
// Display the returned value
// The value is the subtraction of
// 2000 and 1900   
console.log("The value returned by getYear() method: " + n)

Output:

The value returned by getYear() method: 100

Example 3: Here, we pass a full date to the constructor, the format of the date is month-day-year-time, such as October 15, 2010, 02:05:32.

JavaScript
// Full date is assign to the constructor of Date() class
let date = new Date('October 15, 2010, 02:05:32')

// Use "getYear()" method and stores the
// returned value in "n"     
let n = date.getYear();
  
// Display the returned value
// The value is the subtraction of 2010 and 1900   
console.log("The value returned by getYear() method: " + n)

Output
The value returned by getYear() method: 110

Supported Browsers:

  • Google Chrome
  • Mozilla Firefox
  • Opera
  • Safari

JavaScript Date getYear() Method- FAQs

What does the Date.prototype.getYear() method do in JavaScript?

The Date.prototype.getYear() method returns the year of the specified date according to local time, minus 1900.

Why is getYear() considered deprecated?

The getYear() method is deprecated because it can lead to confusion and errors due to its non-standard behavior. It is recommended to use getFullYear() instead.

What should I use instead of getYear()?

You should use the getFullYear() method instead. getFullYear() returns the full year value as a four-digit number, which avoids the confusion caused by getYear().

How does getYear() handle dates in the 21st century?

For dates in the 21st century, getYear() returns values greater than 100. For example, for the year 2023, it returns 123.

What is the most common use of the Date.prototype.getYear() method?

  • Maintaining compatibility with older codebases.
  • Working with dates in legacy systems that use getYear().
  • Migrating legacy code to modern standards by replacing getYear() with getFullYear().

We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.


Previous Article
Next Article

Similar Reads

How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come after some given date(Ex. 'end date' must be after 'start date')
5 min read
How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come before some given date (Ex: 'start date' must come before 'end da
5 min read
How to check the input date is equal to today's date or not using JavaScript ?
Given a date object and the task is to determine whether the given date is the same as today's date or not with the help of JavaScript. There are two well-famous approaches are discussed here. Approach 1: Get the input date from user (var inpDate) and the today's date by new Date(). Now, use .setHours() method on both dates by passing parameters of
3 min read
How to convert UTC date time into local date time using JavaScript ?
Given an UTC date and the task is to convert UTC date time into local date-time using JavaScript toLocaleString() function. Syntax: var theDate = new Date(Date.parse('DATE_IN_UTC')) theDate.toLocaleString() Example 1: This example converts UTC date time into local date time using JavaScript. C/C++ Code <body> <h1 style="color:green;
1 min read
Difference Between new Date() and Date.now() in JavaScript
In JavaScript, handling dates and times is essential for various applications from displaying the current date to performing complex calculations involving time differences. Two commonly used methods for working with the dates are new Date() and Date.now(). Despite dealing with the time they serve different purposes and have distinct behaviors. The
3 min read
Node.js date-and-time Date.isLeapYear() Method
The date-and-time.Date.isLeapYear() is a minimalist collection of functions for manipulating JS date and time module which is used to check if the given year is a leap year or not. Required Module: Install the module by npm or used it locally. By using npm: npm install date-and-time --save By using CDN link: <script src="/path/to/date-and-time.m
2 min read
Node.js date-and-time Date.transform() Method
The date-and-time.Date.transform() method is used to transform date and time from the existing string format to new string format. Required Module: Install the module by npm or used it locally. By using npm:npm install date-and-time --saveBy using CDN link:<script src="/path/to/date-and-time.min.js"></script> Syntax: const transform(dat
2 min read
Node.js date-and-time Date.locale() Method
The date-and-time.Date.locale() method is used to get or switch the locale from one into another language. Required Module: Install the module by npm or used it locally. By using npm: npm install date-and-time --save By using CDN link: <script src="/path/to/date-and-time.min.js"></script> Syntax: locale([code 1="locale" language="[,"][/
1 min read
Node.js date-and-time Date.isSameDay() Method
The date-and-time.Date.isSameDay() method is used to check if the given dates are the same or not. Required Module: Install the module by npm or used it locally. By using npm:npm install date-and-time --saveBy using CDN link:<script src="/path/to/date-and-time.min.js"></script> Syntax: isSameDay(date1, date2) Parameters: This method tak
2 min read
Node.js date-and-time Date.addSeconds() Method
The date-and-time.Date.addSeconds() method is used to add the extra Seconds to the existing date and time. Required Module: Install the module by npm or used it locally. By using npm:npm install date-and-time --saveBy using CDN link:<script src="/path/to/date-and-time.min.js"></script>Syntax: const addSeconds(dateObj, seconds)Parameters
2 min read
three90RightbarBannerImg