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>
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 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>