Home > Web Front-end > JS Tutorial > body text

jQuery gets the difference in date under various browsers_jquery

WBOY
Release: 2016-05-16 18:57:17
Original
965 people have browsed it

If executed under IE:

Copy code The code is as follows:

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:
Copy the code 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:
Copy the code The code is as follows:

/**
* 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.
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!