Get year in JavaScript

The getYear() method returns the year, as a two-digit OR a four-digit number.

Tips and Notes
Note: The value returned by getYear() is not always 4 numbers! For years between 1900 and 1999 the getYear() method returns only two digits. For years before 1900 and after 1999 it returns four digits!
This method is always used in conjunction with a Date object.

Important: The getYear() method should no longer be used. Use the getFullYear() method instead!!

In this example we get the current year and print it:
<script type="text/javascript">
var d = new Date()
The output of the code above will be:


Example 2
Here we will extract the year out of the specific date:
<script type="text/javascript">
var born = new Date("July 21, 1983 01:15:00");
document.write("I was born in " + born.getYear());

No comments:

Post a Comment