gpt4 book ai didi

javascript - 时间戳解析在 IE 中不起作用

转载 作者:行者123 更新时间:2023-11-30 18:28:13 24 4
gpt4 key购买 nike

我有以下格式的时间戳:

[yyyy-MM-ddThh:mm:ssZ](示例:2015-05-15T03:34:17Z)

我想将此时间戳解析为日期,格式如下:[Fri May 15 2015 09:04:17 GMT +5:30]:

现在,我使用以下代码进行解析,它在 Firefox 3.6+ 浏览器中工作正常。但问题是,它在 Internet Explorer (IE) 8 中不起作用,在 IE 中它返回“NaN”

我的 Javascript 代码是:

var myDate = new Date(timestampDate); 
//In Firefox i get myDate as Date object and i can get day, month and year. But in IE this myDate value is coming as NaN

var day = myDate.getDate();
var month = myDate.getMonth();
var year = myDate.getFullYear();

任何帮助都会有返回。请给我一个解决方案,使它在 IE 中也能正常工作。

最佳答案

(function(){
//if the browser correctly parses the test string, use its native method.
var D= new Date('2011-06-02T09:34:29+02:00');

if(D && +D=== 1307000069000) Date.fromISO= function(s){
return new Date(s);
};
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]){
//extract the y-m-d h:m:s.ms digits:
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; //adjust month
//create the GMT date:
day= new Date(Date.UTC.apply(Date, day));
if(!day.getDate()) return NaN;
if(p[5]){
// adjust for the timezone, if any:
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;
}
})();

//测试警报(Date.fromISO("2015-05-15T03:34:17Z").toUTCString())

关于javascript - 时间戳解析在 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10294845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com