Date构造函数在IE中返回NaN,但适用于Firefox和Chrome [英] Date constructor returns NaN in IE, but works in Firefox and Chrome

查看:115
本文介绍了Date构造函数在IE中返回NaN,但适用于Firefox和Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用JavaScript构建一个小日历。我的日期在Firefox和Chrome中工作得很好,但在IE中,日期功能正在返回NaN。



这是功能:

  function buildWeek(dateText){ 
var headerDates ='';
var newDate = new Date(dateText); (var d = 0; d< 7; d ++){
headerDates + ='< th>'+ newDate +'< / th>';


newDate.setDate(newDate.getDate()+ 1);
}

jQuery('div#headerDates')。html('< table>< tr>'+ headerDates +'< / tr>< / table>')
}

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

解决方案

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



设置日期的可靠方法是构造一个日期,并使用 setFullYear setTime 方法。



这里显示的示例如下:
http://jibbering.com/faq/#parseDate



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




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

  / **将格式为YYYY-MM-DD的字符串解析为Date对象。 
*如果提供的字符串与格式不符,则返回
*无效的Date(值NaN)。
* @param {string} dateStringInRange格式YYYY-MM-DD,年份为
*范围为0000-9999(含)。
* @return {Date}表示字符串的日期对象。
* /

函数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);
}
}
返回日期;
}


I'm trying to build a little calendar in JavaScript. I have my dates working great in Firefox and Chrome, but in IE the date functions are returning NaN.

Here is the function :

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>');
}

dateText is the Monday of the current week which is actually set in php in the format of 'm, d, Y', e.g. "02, 01, 2010".

解决方案

The Date constructor accepts any value. If the primitive [[value]] of the argument is number, then the Date that is created has that value. If primitive [[value]] is String, then the specification only guarantees that the Date constructor and the parse method are capable of parsing the result of Date.prototype.toString and Date.prototype.toUTCString()

A reliable way to set a Date is to construct one and use the setFullYear and setTime methods.

An example of that appears here: http://jibbering.com/faq/#parseDate

ECMA-262 r3 does not define any date formats. Passing string values to the Date constructor or Date.parse has implementation-dependent outcome. It is best avoided.


Edit: The entry from comp.lang.javascript FAQ is: An Extended ISO 8601 local date format YYYY-MM-DD can be parsed to a Date with the following:-

/**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;
  }

这篇关于Date构造函数在IE中返回NaN,但适用于Firefox和Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆