Solve the problem of invalid date for the value of new date () for IE, Firefox browser JS

Source: Internet
Author: User

The new date ("XXXX") method is often required when we need to convert a string of date strings into a specific date format.

At that time, under IE browser, you will encounter this problem:

New Date ('2016-01-01 00:00:00')    // returns this value invalid date, conversion failed

But this method can return the correct results on Google Chrome.

Workaround: The final discovery is that the format of the string is not caused by some browsers failing.

New Date (' 2016/01/01 00:00:00 ')  //

When used in a specific way, you can convert the date string to the correct format by using some string conversions, and then use the new Date ().

var date= "2016-12-12 10:10:10";d ate=date.replace (/-/, "/"); Date D=new date (date);

So, there are different browsers, and the following list shows how all browsers support it.

1 varD =NewDate (2011, 01, 07);//yyyy, MM-1, DD2 varD =NewDate (2011, 01, 07, 11, 05, 00);//yyyy, MM-1, DD, hh, MM, SS3 varD =NewDate ("02/07/2011");//"Mm/dd/yyyy"4 varD =NewDate ("02/07/2011 11:05:00");//"Mm/dd/yyyy hh:mm:ss"5 varD =NewDate (1297076700000);//milliseconds6 varD =NewDate ("Mon-Feb-11:05:00 GMT");//"Day Mon dd yyyy hh:mm:ss GMT/UTC

Solve the problem of invalid date for the value of new date () for IE, Firefox browser JS

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.