Home >Web Front-end >JS Tutorial >Analysis of the difference between getYear() and getFullYear() in JS_time and date

Analysis of the difference between getYear() and getFullYear() in JS_time and date

WBOY
WBOYOriginal
2016-05-16 16:42:321169browse

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);

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);
}
Statement:
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