Browser compatibility issues with date functions new Date () in JavaScript _javascript tips

Source: Internet
Author: User

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 '.

Related Article

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.