JQuery various browsers get date difference _jquery

Source: Internet
Author: User
Tags getdate
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.