【问题标题】:Javascript JSON Date parse in IE7/IE8 returns NaNIE7/IE8 中的 Javascript JSON 日期解析返回 NaN
【发布时间】:2012-06-16 18:02:44
【问题描述】:

我正在从 JSON 事件源解析日期 - 但日期在 IE7/8 中显示为“NaN”:

// Variable from JSON feed (using JQuery's $.getJSON)
var start_time = '2012-06-24T17:00:00-07:00';

// How I'm currently extracting the Month & Day
var d = new Date(start_time);
var month = d.getMonth();
var day = d.getDate();

document.write(month+'/'+day);// "6/24" in most browsers, "Nan/Nan" in IE7/8

我做错了什么?谢谢!

【问题讨论】:

标签: javascript date internet-explorer-8 internet-explorer-7 utc


【解决方案1】:

在较旧的浏览器中,您可以编写一个函数来为您解析字符串。

这个创建了一个 Date.fromISO 方法——如果浏览器可以从 ISO 字符串中本地获取正确的日期,则使用本地方法。

一些浏览器部分正确,但返回了错误的时区,所以只检查 NaN 可能不行。

Polyfill:

(function(){
    var D= new Date('2011-06-02T09:34:29+02:00');
    if(!D || +D!== 1307000069000){
        Date.fromISO= function(s){
            var day, tz,
            rx=/^(\d{4}\-\d\d\-\d\d([tT ][\d:\.]*)?)([zZ]|([+\-])(\d\d):(\d\d))?$/,
            p= rx.exec(s) || [];
            if(p[1]){
                day= p[1].split(/\D/);
                for(var i= 0, L= day.length; i<L; i++){
                    day[i]= parseInt(day[i], 10) || 0;
                };
                day[1]-= 1;
                day= new Date(Date.UTC.apply(Date, day));
                if(!day.getDate()) return NaN;
                if(p[5]){
                    tz= (parseInt(p[5], 10)*60);
                    if(p[6]) tz+= parseInt(p[6], 10);
                    if(p[4]== '+') tz*= -1;
                    if(tz) day.setUTCMinutes(day.getUTCMinutes()+ tz);
                }
                return day;
            }
            return NaN;
        }
    }
    else{
        Date.fromISO= function(s){
            return new Date(s);
        }
    }
})()

结果:

var start_time = '2012-06-24T17:00:00-07:00';
var d =  Date.fromISO(start_time);
var month = d.getMonth();
var day = d.getDate();

alert(++month+' '+day); // returns months from 1-12

【讨论】:

  • 这是在 IE8 中返回 5/24 而不是 6/24 能否请您更新该功能以正常工作???
  • 没有改变函数,只是在警报中增加了 date.getMonth() 以返回基于 1 的月份。
  • 这很好用,谢谢。如果您能稍微评论一下代码以帮助我们理解您为什么做了某些事情,例如,我们将不胜感激。 +D!==1307000069000
  • @jackocnr 这也花了我一分钟。 + 将 D 转换为一个数字。如果是有效日期,则与 D.getTime() 或 1307000069000 相同。否则为 NaN。不是很可读,但很聪明。
  • 正则表达式应该更新为:rx=/^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*) ?)([zZ]|([+\-])(\d\d):?(\d\d))?$/ 在最后一个冒号后添加问号,使其可选。时区偏移量通常不包括冒号。 (例如:“-400”、“+0000”)这是 PHP 中常量 DateTime::ISO8601 的格式,不会产生冒号。
【解决方案2】:

对于 ie7/8 我刚刚做了:

var ds = yourdatestring;
ds = ds.replace(/-/g, '/');
ds = ds.replace('T', ' ');
ds = ds.replace(/(\+[0-9]{2})(\:)([0-9]{2}$)/, ' UTC\$1\$3');
date = new Date(ds);

这会将所有出现的“-”替换为“/”,将时间标记“T”替换为空格,并将时区信息替换为 IE 友好的字符串,使 IE7/8 能够正确解析字符串中的日期。为我解决了所有问题。

【讨论】:

  • 不,它会返回不同的日期实例。在 Chrome 和 IE8 中比较 new Date('2013/01/01')。正确的应该是new Date('2013-01-01Z)(注意最后的 Z)。
  • 在我之前的帖子中提到“For ie7/8”。
  • 你说得对,'Z' 仅在 Chrome 中使用斜线时才重要,抱歉。我们只是在所有浏览器中用斜线替换破折号以避免浏览器检测。所以我们需要'Z`。
  • 我在 IE8 上遇到了同样的问题,.replace('-', '/') 解决了这个问题。非常感谢!
  • 不。在 IE8 中,这给了我一个字符串“2015/01/12T08:05:02.68”,它转换成一个“无效日期”值。
【解决方案3】:

Result of toJSON() on a date is different between IE8 and IE9+ 上查看 RobG 的帖子。

以下函数在 IE 8 及更低版本中对我有用。

// parse ISO format date like 2013-05-06T22:00:00.000Z
function convertDateFromISO(s) {
  s = s.split(/\D/);
  return new Date(Date.UTC(s[0], --s[1]||'', s[2]||'', s[3]||'', s[4]||'', s[5]||'', s[6]||''))
}

你可以像下面这样测试:

var currentTime = new Date(convertDateFromISO('2013-05-06T22:00:00.000Z')).getTime();
alert(currentTime);

【讨论】:

  • 请注意,这不适用于问题中格式的日期。时间戳必须使用00.000Z 格式,问题中的-07:00 不起作用。
  • @breckenedge,你是对的。它必须使用 ISO 格式00.000Z
【解决方案4】:

我建议 http://momentjs.com/ 解决跨浏览器日期问题。

【讨论】:

  • moment.js 是一个不错的选择。我的问题是尺寸,它的大部分周长都专门用于处理我不关心的事情。 (例如,日期时间戳中的希伯来语字符)
【解决方案5】:

@gib 感谢您对 Moment.js 的建议。这个小型库确实有助于处理日期和 JavaScript。

Moment.js 解决了我也遇到的原始问题中描述的问题。 IE8 在解析为新的 Date() 对象时将 JSON ISO 日期显示为 NaN。

快速解决方案(在你的页面中包含moment.js,或者将代码复制到你的js函数中包含)

如果您只需要在页面上显示从 JSON ISO 日期加载的日期,请执行以下操作:

order_date = moment(data.OrderDate); //create a "moment" variable, from the "data" object in your JSON function in Protoype or jQuery, etc.

$('#divOrderDate).html(order_date.calendar()); //use Moment's relative date function to display "today", "yesterday", etc.

order_date = moment(data.OrderDate); //create a "moment" variable, from the "data" object in your JSON function in Protoype or jQuery, etc.

$('#divOrderDate).html(order_date.format('m/d/YYYY')); //use Moment's format function to display "2/6/2015" or "10/19/2014", etc.  

如果您必须有一个 Date() 对象(例如用于 jQuery 组件),请执行以下操作以成功填充 JSON 提供的 ISO 日期。 (这假设您已经在处理 JSON 数据的函数中。)

var ship_date = new Date(moment(data.ShipDate).format('m/d/YYYY'));  //This will successfully parse the ISO date into JavaScript's Date() object working perfectly in FF, Chrome, and IE8.

//initialize your Calendar component with the "ship_date" variable, and you won't see NaN again.

【讨论】:

    猜你喜欢
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 2014-07-01
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    相关资源
    最近更新 更多