SoFunction
Updated on 2025-03-10

Compatible with IE, firefox and chrome js get time (getFullYear)

Generally speaking, when we obtain the year, we use the following code:

var now = new Date();  
var initYear = (); 

The above code is correct in IE, but it is incompatible with ff and chrome.

var initYear = ();

IE, FireFox, and Chrome are all feasible

The difference between getYear() and getFullYear() in JS:

The method of getting the current year in js is to get the year var dayObj=new Date(); () to get the year. I have written before that there will be a problem with browser compatibility, that is, we can get the results we want in IE but it won't work in FF, which is 1900 years different from the results we want. My approach at that time was:

var dayObj=new Date();
var myYears = ( () < 1900 ) ? ( 1900 + () ) : ();
(myYears);

This will avoid compatibility issues between IE and FF.

Now I see that there is such a method in js getFullYear(). After testing, it turns out that this method can avoid the problems above, and both IE and FF can be displayed as we want.

getFullYear method
Returns the year value in the Date object represented by local time.

()

The required dateObj parameter is a Date object.

illustrate
To get the year value expressed in Global Standard Time (UTC), use the getUTCFullYear method.

The getFullYear method returns the year value as an absolute number. For example, the return value of 1976 is 1976. This avoids the 2000 problem, and thus does not confuse dates after January 1, 2000 with dates after January 1, 1900.

The following example illustrates the usage of the GetFullYear method.

function DateDemo(){  
var d, s = "Today's UTC date is: ";  
d = new Date();  
s += (() + 1) + "/";  
s += () + "/";  
s += ();  
return(s);
}