[Problem phenomenon]:
When we need to convert a string of date strings into a specific Date format, we often need to use the new Date("yyyy-mm-dd hh:mm:ss") method. Under IE and Firefox browsers, we will encounter to this kind of question:
new Date('2017-01-20 00:00:00') //It returns this value Invalid Date, the conversion fails
But this method returns correct results on Google Chrome.
【Cause Analysis】:
Different browsers still have differences in different time formats. The following lists the methods supported by all browsers:
var d = new Date(2017, 01, 07); var d = new Date(2017, 01, 07, 11, 05, 00); var d = new Date("01/07/2017"); var d = new Date("01/07/2016 11:05:00"); var d = new Date(1297076700000); var d = new Date("Mon Feb 07 2011 11:05:00 GMT");
【solution】:
You can use regular expressions to convert the format, or you can use the correct format when passing values from the background to the foreground:
new Date('2017/01/20 00:00:00');
var date="2017-01-20 10:10:10"; date=date.replace(new RegExp(/-/gm) ,"/"); //Convert all '-' to '/' Date d=new Date(date);
Donor sharer
I didn't like programming before, but now I am an IT fan obsessed with programming. I would like to share some things I have sorted out and optimized here, hoping to help IT fans, with joy and sweat, and at the same time I also hope that everyone can support it. Of course, if you have money to support a money field (support Alipay and WeChat donations, join the it data center buckle group), but have no money to support a personal field, with your support, we will be more motivated and do better, thank you Ladies and gentlemen.