JavaScript getYear Function

JavaScript getYear function is one of the Date Function, which returns the current year of a given date minus 1900. The syntax of the JavaScript getYear function is:

 Date.getYear()

JavaScript getYear Function Example

We use the getYear function to return the year (year – 1900) in a current date and time.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Get Year Function </title>
</head>
<body>
    <h1> JavaScript getYear Function Example </h1>
<script>
  var dt = Date();  
  document.write("Date and Time : " + dt);
  document.write("Get Year using getYear() : " + dt.getYear());
</script>
</body>
</html>
JavaScript getYear Example

Date and Time: Mon Nov 05 2018 11:32:15 GMT+0530 (Indian Standard Time)
Get Year using getYear() : NaN

This JavaScript get year function example returns the year from the user specified custom date minus 1900

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Get Year Function </title>
</head>
<body>
    <h1> JavaScript getYear Function Example </h1>
<script>
  var dt = Date("April 1, 2012 10:12:22.0716");
  document.write("Date and Time : " + dt);
  document.write("Get Year using getYear() : " + dt.getYear());
</script>
</body>
</html>
JavaScript getYear Function 2

JavaScript get Year Example 2

In this getYear function example, we extract the year from the date without year. Or, we can say, get year from time.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Get Year Function </title>
</head>
<body>
    <h1> JavaScript getYear Function Example </h1>
<script>
  var dt = Date("10:12:22.0716");
  document.write("Date and Time : " + dt);
  document.write("Get Year using getYear() : " + dt.getYear());
</script>
</body>
</html>
JavaScript getYear Example

Date and Time : Invalid Date
Get Year using getYear() : NaN