Home > Web Front-end > JS Tutorial > Compatible with IE, firefox and chrome js acquisition time (getFullYear)_time and date

Compatible with IE, firefox and chrome js acquisition time (getFullYear)_time and date

WBOY
Release: 2016-05-16 16:42:34
Original
1559 people have browsed it

Generally, when we get the year, we use the following code:

var now = new Date();  
var initYear = now.getYear(); 
Copy after login

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();
Copy after login

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 ) &#63; ( 1900 + dayObj.getYear() ) : dayObj.getYear();
document.write(myYears);

Copy after login

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);
}
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template