日期构造函数在IE中返回NaN,但在Firefox和Chrome中有效

ped*_*ete 78 javascript internet-explorer date

我正在尝试用JavaScript构建一个小日历.我的日期在Firefox和Chrome中运行良好,但在IE中,日期函数返回NaN.

这是功能:

function buildWeek(dateText){
    var headerDates='';
    var newDate = new Date(dateText);

    for(var d=0;d<7;d++){
        headerDates += '<th>' + newDate + '</th>';
        newDate.setDate(newDate.getDate()+1);
    }                       

    jQuery('div#headerDates').html('<table><tr>'+headerDates+'</tr></table>');
}
Run Code Online (Sandbox Code Playgroud)

dateText是本周的星期一,它实际上是以'm,d,Y'的格式在php中设置的,例如"02, 01, 2010".

Qli*_*max 85

从mysql datetime/timestamp格式:

var dateStr="2011-08-03 09:15:11"; //returned from mysql timestamp/datetime field
var a=dateStr.split(" ");
var d=a[0].split("-");
var t=a[1].split(":");
var date = new Date(d[0],(d[1]-1),d[2],t[0],t[1],t[2]);
Run Code Online (Sandbox Code Playgroud)

我希望对某人有用.适用于IE FF Chrome

  • 谢谢,正是我需要的. (2认同)

Gar*_*ett 66

Date构造函数接受任何值.如果参数的原始[[value]]是数字,则创建的日期具有该值.如果primitive [[value]]是String,那么规范只保证Date构造函数和parse方法能够解析Date.prototype.toString和Date.prototype.toUTCString()的结果.

设置Date的可靠方法是构造一个并使用setFullYearsetTime方法.

这方面的一个例子出现在这里:http: //jibbering.com/faq/#parseDate

ECMA-262 r3未定义任何日期格式.将字符串值传递给Date构造函数或Date.parse具有依赖于实现的结果.最好避免.


编辑: comp.lang.javascript FAQ中的条目是:扩展的ISO 8601本地日期格式YYYY-MM-DD可以解析为a,Date具有以下内容: -

/**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;
  }
Run Code Online (Sandbox Code Playgroud)

  • 感谢指出,疯了为什么chrome工作正常解析日期而ie7说NAN,好东西$ .datepicker.parseDate来自jquery能够解析日期 (3认同)

diy*_*ism 15

不要使用"new Date()",因为它将输入日期字符串作为本地时间:

new Date('11/08/2010').getTime()-new Date('11/07/2010').getTime();  //90000000
new Date('11/07/2010').getTime()-new Date('11/06/2010').getTime();  //86400000
Run Code Online (Sandbox Code Playgroud)

我们应该使用"NewDate()",它将输入作为GMT时间:

function NewDate(str)
         {str=str.split('-');
          var date=new Date();
          date.setUTCFullYear(str[0], str[1]-1, str[2]);
          date.setUTCHours(0, 0, 0, 0);
          return date;
         }
NewDate('2010-11-07').toGMTString();
NewDate('2010-11-08').toGMTString();
Run Code Online (Sandbox Code Playgroud)


mag*_*ker 7

这是另一种为Date对象添加方法的方法

用法: var d = (new Date()).parseISO8601("1971-12-15");

    /**
     * Parses the ISO 8601 formated date into a date object, ISO 8601 is YYYY-MM-DD
     * 
     * @param {String} date the date as a string eg 1971-12-15
     * @returns {Date} Date object representing the date of the supplied string
     */
    Date.prototype.parseISO8601 = function(date){
        var matches = date.match(/^\s*(\d{4})-(\d{2})-(\d{2})\s*$/);

        if(matches){
            this.setFullYear(parseInt(matches[1]));    
            this.setMonth(parseInt(matches[2]) - 1);    
            this.setDate(parseInt(matches[3]));    
        }

        return this;
    };