//兼容IE8以下的写法 var birthDay = '1992-02-22'; var birthDay = new Date(birthDay.replace(/-/g, '/'));
PS:IE8 new Date('1991/23/44')不会报错,他会进位,而chrome下则认为这样的日期是错误的,会报错。那么有一种方法可以帮助我们判断某个日期字符串是否正确。如下代码:
var birthDay = '1992-22-44'; var birthday = new Date(birthDay.replace(/-/g, '/')); if(birthday.toString() === 'Invalid Date') { return false; } //ie下 var birthDayArr = birthDay.split('-'); if(birthday.getFullYear() != birthDayArr[0] || (birthday.getMonth()+1 != birthDayArr[1]) || birthday.getDate() != birthDayArr[2]) { return false; }
转载于:https://blog.51cto.com/5310331/1395766