javascript下ie7,ie8的Date Bug的解决

来源:互联网 发布:在家做网络兼职 编辑:程序博客网 时间:2024/06/08 18:24

ie9+, chrome firefox opera下 string到Date 使用   Date("2013-01-01"); 都是ok的。

但在ie7, ie8下 返回NaN


国外有人写了这样一个解决办法

/**Parses string formatted as YYYY-MM-DD to a Date object.  * If the supplied string does not match the format, an  * invalid Date (value NaN) is returned.  * @param {string} dateStringInRange format YYYY-MM-DD, with year in  * range of 0000-9999, inclusive.  * @return {Date} Date object representing the string.  */ function parseISO8601(dateStringInRange) {   var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,       date = new Date(NaN), month,       parts = isoExp.exec(dateStringInRange);   if(parts) {     month = +parts[2];     date.setFullYear(parts[1], month - 1, parts[3]);     if(month != date.getMonth() + 1) {       date.setTime(NaN);     }   }   return date; }

使用如下:

parseISO8601("2013-01-01");

原创粉丝点击