javascript下ie7,ie8的Date Bug的解决 .

来源:互联网 发布:重庆正规网络整合营销 编辑:程序博客网 时间:2024/06/01 23:56


转自:http://blog.csdn.net/fjh658/article/details/8524530


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

但在ie7, ie8下 返回NaN


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

[javascript] view plaincopyprint?
  1. /**Parses string formatted as YYYY-MM-DD to a Date object. 
  2.   * If the supplied string does not match the format, an 
  3.   * invalid Date (value NaN) is returned. 
  4.   * @param {string} dateStringInRange format YYYY-MM-DD, with year in 
  5.   * range of 0000-9999, inclusive. 
  6.   * @return {Date} Date object representing the string. 
  7.   */  
  8.  function parseISO8601(dateStringInRange) {  
  9.    var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,  
  10.        date = new Date(NaN), month,  
  11.        parts = isoExp.exec(dateStringInRange);  
  12.   
  13.    if(parts) {  
  14.      month = +parts[2];  
  15.      date.setFullYear(parts[1], month - 1, parts[3]);  
  16.      if(month != date.getMonth() + 1) {  
  17.        date.setTime(NaN);  
  18.      }  
  19.    }  
  20.    return date;  
  21.  }  

使用如下:

parseISO8601("2013-01-01");

0 0
原创粉丝点击