Js time of getFullYear compatible with IE firefox and chrome

  • 2020-03-30 03:30:40
  • OfStack

Generally, we get the year through the following code:

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

The above code is correct in IE initYear, but not compatible with ff and chrome

var initYear = now.getFullYear();

Internet explorer, FireFox, and Chrome are all available

Differences between getYear() and getFullYear() in JS:

Var dayObj=new Date(); Dayobj. getYear() to get the year, which I wrote about earlier that would cause browser compatibility problems, which is that we get the results we want in IE but not in FF, 1900 years from what we want. And what I did was:

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

This will avoid compatibility problems with IE and FF.

Now I see a getFullYear() method in js. Let's test it out. It turns out that this method can avoid the problems above. 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.

To get a year value 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 for 1976 is 1976. This avoids the y2k problem of confusing dates after 1 January 2000 with dates after 1 January 1900.

The following example illustrates the use of the GetFullYear method.

function DateDemo(){  
var d, s = " Today,  UTC  The date is : ";  
d = new Date();  
s += (d.getMonth() + 1) + "/";  
s += d.getDate() + "/";  
s += d.getFullYear();  

Related articles: