If you are executing under IE:
Copy Code code as follows:
var currentdate = new Date ();
Alert (Currentdate.getyear ());
Will pop 2008, but under FF is 108, why?
First, let's take a look at Greenwich Mean Time (GMT), which starts in 1900, and we'll take a look at this expression: 108 + 1900 = 2008
The reason is that FF did not add the year 1900, and then the code is as follows:
Copy Code code as follows:
/**
* Get the current date
*
* @return {}
*/
function Getcurrentdate () {
var useragent = Navigator.userAgent.toLowerCase ();
Because IE's year is 2008 and FF is 108, judge
var currentyear = Currentdate.getyear ();
if ($.browser.mozilla) {
CurrentYear + 1900;
}
var currentdatestr = currentyear + '-' + (Currentdate.getmonth () + 1) + '-' + currentdate.getdate ();
return currentdatestr;
};
Problem solved, Test succeeded
And then run the system under Google Browser Chrome and ran into the same problem ...
Everybody look at this judgment:
if ($.browser.mozilla)
Here to determine whether the FF browser, the above code has passed the test, then Google browser what to do?
I also made a judgment:
var useragent = Navigator.userAgent.toLowerCase ();
var chrome =/chrome/.test (useragent);
This applies jquery's browser-judging method, using regular expressions to get a series of browser parameters, and then query for a chrome string, there is Google's browser, so the final code is:
Copy Code code as follows:
/**
* Get the current date
*
* @return {}
*/
function Getcurrentdate () {
var useragent = Navigator.userAgent.toLowerCase ();
To determine if Google's browser
var chrome =/chrome/.test (useragent);
var currentdate = new Date ();
Because IE's year is 2008 and FF is 108, judge
var currentyear = Currentdate.getyear ();
if ($.browser.mozilla | | chrome) {
CurrentYear + 1900;
}
var currentdatestr = currentyear + '-' + (Currentdate.getmonth () + 1) + '-'
+ currentdate.getdate ();
return currentdatestr;
};
Other browsers can be pushed logically.
The last thing to note is the way to get the month: Currentdate.getmonth () + 1, because the date was originally designed from 0, so we need to add one to the month of acquisition.