If executed under IE:
var currentDate = new Date( );
alert(currentDate.getYear());
will pop up 2008, but under FF it is 108. Why is this?
First of all, let’s understand the “Greenwich Mean Time (GMT)” time. It starts from 1900. Let’s take a look at this operation expression: 108 1900 = 2008
The reason is that FF does not add the year 1900. Then the code is as follows:
/**
* Get the current date
*
* @return {}
*/
function getCurrentDate() {
var userAgent = navigator.userAgent.toLowerCase();
// Since the year of IE is 2008 and FF is 108, determine
var currentYear = currentDate.getYear() ;
if ($.browser.mozilla) {
currentYear = 1900;
}
var currentDateStr = currentYear '-' (currentDate.getMonth() 1) '-' currentDate.getDate() ;
return currentDateStr;
};
The problem was solved and the test was successful
Later, I ran the system under GOOGLE browser chrome and encountered the same problem...
Look at this judgment:
if ($.browser.mozilla)
Here it is judged whether it is a FF browser. The above code has passed the test, so what about the GOOGLE browser?
Similarly, I also made a judgment:
var userAgent = navigator.userAgent.toLowerCase();
var chrome = /chrome/.test(userAgent);
The browser judgment of jQuery is applied here Method, use regular expressions to obtain a series of browser parameters, and then query whether there is a chrome string. If there is a GOOGLE browser, the final code is:
/**
* Get the current date
*
* @return {}
*/
function getCurrentDate() {
var userAgent = navigator.userAgent.toLowerCase ();
//Determine whether it is Google’s browser
var chrome = /chrome/.test(userAgent);
var currentDate = new Date();
// Due to the year of IE is 2008 and FF is 108, determine
var currentYear = currentDate.getYear();
if ($.browser.mozilla || chrome) {
currentYear = 1900;
}
var currentDateStr = currentYear '-' (currentDate.getMonth() 1) '-'
currentDate.getDate();
return currentDateStr;
};
Other browsers follow Logical reasoning is enough
The last thing to note is the method of obtaining the month: currentDate.getMonth() 1. Because the date starts from 0 when it was originally designed, we have to add one to the obtained month.