JavaScript tutorial:
getYear method

 

Applies to: Date Object

The getYear method is use to get the year value in a Date object.

Syntax

objDate.getYear( )

Return

Returns the year value in a Date object.

Example

This method is obsolete, and is provided for backwards compatibility only. Use the getFullYear method instead. For the current century, the year is a 2-digit integer value returned as the difference between the stored year and 1900. For dates other than the current century, the 4-digit year is returned. For example, 1996 is returned as 96, but 1825 and 2025 are returned as-is. The following example illustrates the use of the getYear method:

s = "Today's date is: ";
d = new Date();
s += (d.getMonth() + 1) + "/";
s += d.getDate() + "/";
s += d.getFullYear();

document.write(s);

  To run the code above, paste it into JavaScript Editor, and click the Execute button

See also: Date Object Methods, getFullYear Method, getUTCFullYear Method, setFullYear Method, setUTCFullYear Method, setYear Method