In js, the current year is obtained using var dayObj = new Date (); dayObj. getYear () is used to get the year. I have previously mentioned that browser compatibility issues may occur, that is, we can get the expected results in IE, but it won't work in FF, the result is different from what we want in 1900. At that time, my practice was:
Var dayObj = new Date (); var myYears = (dayObj. getYear () <1900 )? (1900 + dayObj. getYear (): dayObj. getYear (); document. write (myYears );
In this way, the compatibility between IE and FF can be avoided.
Now I see that js has this method getFullYear (). After testing, this method can avoid the above problems. Both IE and FF can be displayed as we want.
GetFullYear Method
Returns the year value represented by the local time in the Date object.
DateObj. getFullYear ()
The required dateObj parameter is a Date object.
Description
To obtain the Year Value in UTC, use the getUTCFullYear method.
The getFullYear method returns the year value as an absolute number. For example, the 1976 return value is 1976. This avoids the issue of July 22, 2000, and does not confuse the date after July January 1, 2000 with the date after July January 1, 1900.
The following example shows how to use the GetFullYear method.
Function DateDemo () {var d, s = "Today's UTC Date is:"; d = new Date (); s + = (d. getMonth () + 1) + "/"; s + = d. getDate () + "/"; s + = d. getFullYear (); return (s );}