Generally, when we get the year, we use the following code:
var now = new Date(); var initYear = now.getYear();
The above code is correct in initYear in IE, but it is not compatible under ff and chrome. Replace it with
var initYear = now.getFullYear();
Available under IE, FireFox and Chrome
The difference between getYear() and getFullYear() in JS:
The way to get the current year in js is var dayObj=new Date(); dayObj.getYear() to get the year. I wrote before that this will cause browser compatibility problems, that is, in IE we can get what we want The desired result does not work in FF, and there is a difference of 1900 years between the desired result and the desired result. What I did at that time was:
var dayObj=new Date(); var myYears = ( dayObj.getYear() < 1900 ) ? ( 1900 + dayObj.getYear() ) : dayObj.getYear(); document.write(myYears);
This way you can avoid the compatibility issues between IE and FF.
Now I see that there is such a method getFullYear() in js. After testing, it turns out that this method can avoid the above problems, and both IE and FF can display as we want.
getFullYear method
Returns the year value expressed in local time in a Date object.
dateObj.getFullYear()
The required dateObj parameter is a Date object.
Description
To get the year value in Coordinated Universal Time (UTC), use the getUTCFulYear method.
The getFullYear method returns the year value as an absolute number. For example, the return value for the year 1976 is 1976. This avoids the year 2000 problem, whereby dates after January 1, 2000, are not confused with dates after January 1, 1900.
The following example illustrates the usage of the GetFullYear method.
function DateDemo(){ var d, s = "今天 UTC 日期是: "; d = new Date(); s += (d.getMonth() + 1) + "/"; s += d.getDate() + "/"; s += d.getFullYear(); return(s); }