Javascript JSON IE7 / IE8中的日期解析返回NaN [英] Javascript JSON Date parse in IE7/IE8 returns NaN

查看:110
本文介绍了Javascript JSON IE7 / IE8中的日期解析返回NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从JSON事件提要中解析日期,但日期显示在IE7 / 8中的NaN:

  //从JSON feed变量(使用JQuery的$ .getJSON)
var start_time ='2012-06-24T17:00:00-07:00'

//我当前正在提取月份& day
var d = new Date(start_time);
var month = d.getMonth();
var day = d.getDate();

document.write(month +'/'+ day); //6/24在大多数浏览器中,Nan / Nan在IE7 / 8

我做错了什么?谢谢!

解决方案

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



这个创建一个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())返回NaN;
if(p [5]){
tz =(parseInt(p [5],10)* 60);
if(p [6])tz + = parseInt(p [6],10);
if(p [4] =='+')tz * = -1;
如果(tz)day.setUTCMinutes(day.getUTCMinutes()+ tz);
}
返回日
}
返回NaN;
}
}
else {
Date.fromISO = function(s){
return new Date;
}
}
})()

结果: / p>

  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); //返回月份从1-12


I'm parsing a date from a JSON event feed - but the date shows "NaN" in IE7/8:

// 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

What am I doing wrong? Thanks!

解决方案

In older browsers, you can write a function that will parse the string for you.

This one creates a Date.fromISO method- if the browser can natively get the correct date from an ISO string, the native method is used.

Some browsers got it partly right, but returned the wrong timezone, so just checking for NaN may not do.

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

Result:

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

这篇关于Javascript JSON IE7 / IE8中的日期解析返回NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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