The same language JavaScript, in different browsers, there is a language compatibility problem, essentially because different browsers are supported language standards and implementation of the differences. This article will create a Date object based on the new date to parse the problem.
1. The problem of the proposed, start time and end time space can not correctly pass the value
In the page, we used a time component to develop the time selection box, but found that under Firefox is not working properly, under the chrome can be normal operation. What's the problem?
2. Problem analysis
The result analysis was found to be a problem with the following code:
var timestart = ' 2010-05-04 ';
var timeend = ' 2015-09-05 ';
var time1 = (timestart+ ' 00:00:00 '). toString ();
var time2 = (timeend+ ' 23:59:59 '). toString ();
Timestart = new Date (time1);
Timeend = new Date (time2);
The problem is that the new date (time1) constructor does not generate the Date object correctly, and its value is Nan. Strange, where is the problem?
3. Performance on various browsers
Implementation under IE:
Implementation under Firefox:
Under the chrome implementation situation:
From the above analysis, you can tell that this JavaScript script can be executed correctly under Chrome, but in other browsers, the error is reported.
4. The right approach
The following are the correct practices:
var time1 = (timestart+ ' 00:00:00 '). toString ();
var time2 = (timeend+ ' 23:59:59 '). toString ();
Timestart = new Date (Date.parse (Time1.replace (/-/g, "/")). GetTime ();
Timeend = new Date (Date.parse (Time2.replace (/-/g, "/")). GetTime ();
The main change is the default date format conversion, based on the '/' format of the date string, is widely supported by various browsers, '-' connected to the date string, it is only under the chrome can work correctly.
5. Summary of Knowledge points
' 2015-09-05 ' is not available in individual browsers, using new Date (str) to correctly generate date objects. The correct usage is ' 2015/09/06 '.