JavaScript | date.getYear() Function

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

Syntax:

date.getYear()

Here, date is the object of Date() class.



Parameter: This function does not accept any parameters.

Return Value: This method returns a value which is subtracted from 1900 to current year.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// "date" is object of Date() class
var date = new Date();
  
// Use "getYear()" method and stroes 
// the returned value to "n"
var n = date.getYear();
  
// Display the result
document.write("The value returned by getYear() method: " + n)
  
</script>                    

chevron_right


Output:

The value returned by getYear() method: 119

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// Year 2000 is assign to the 
// constructor of Date() class
var date = new Date('2000')
    
// Use "getYear()" method and stroes
// the returned value to "n"
var n = date.getYear();
    
// Display the returned value
// The value is the subtraction of
// 2000 and 1900   
document.write("The value returned by getYear() method: " + n)
    
</script>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// Full date is assign to the constructor of Date() class
var date = new Date('October 15, 2010, 02:05:32')
  
// Use "getYear()" method and stroes the
// returned value in "n"     
var n = date.getYear();
    
// Display the returned value
// The value is the subtraction of 2010 and 1900   
document.write("The value returned by getYear() method: " + n)
    
</script>

chevron_right


Output:

The value returned by getYear() method: 110


My Personal Notes arrow_drop_up

2nd year student of Information Technology JADAVPUR UNIVERSITY

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.