如何让 Internet Explorer 解析“2010-06-09T15:20:00Z”作为约会?

发布于 2024-11-29 14:54:52 字数 589 浏览 0 评论 0原文

我正在使用 JavaScript 来处理组合 UTC 格式的日期(例如“2010-06-09T15:20:00Z”)。 IE 没有给出预期的结果(震惊)。以下产生 NaN。

var d = new Date("2010-06-09T15:20:00Z");  // NaN

然而,微软在此页面上的文档说这应该可以工作(就像在 FF 中一样)。我什至从本页复制了 JScript 代码,但它无法运行,并出现与我在工作中发现的相同错误。 (有点让你怀疑 MS 在发布之前是否对其进行了测试,或者 IE 是否由于补丁而损坏)。

格式化日期和时间字符串 (msdn.microsoft .com)

我可以通过删除“T”并将破折号切换为斜杠来让 IE 解析类似的字符串。这样做的问题是我失去了时间的偏移逻辑。

有关如何在 IE 中解决此问题的任何建议。我已经在 IE6 和 IE7(兼容模式)中对此进行了测试。我没有其他 Windows 系统可供检查。

I am using JavaScript to work with dates in combined UTC format (e.g. "2010-06-09T15:20:00Z"). IE is not giving expected results (shock). The following yields NaN.

var d = new Date("2010-06-09T15:20:00Z");  // NaN

However Microsoft's documentation on this page says this should work (like it does in FF). I have even copied the JScript code off of this page and it fails to run giving the same error I am finding in my work. (Kind of makes you wonder if MS testing it before posting or if IE is broken due to a patch).

Formatting Date and Time String (msdn.microsoft.com)

I can get IE to parse similar strings by removing the 'T' and switching the dashes to slashes. The problem with this is that I loose the time's offset logic.

Any suggestions on how to work around this problem in IE. I have tested this in IE6 and IE7 (compatibility mode). I don't have another Windows system to check on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

棒棒糖 2024-12-06 14:54:52

对于无麻烦的日期操作,我喜欢使用 Datejs - 开源 JavaScript 日期库,它做了很多事情日期很棒,但使用它的主要成本是 26kb 文件大小。

它将解析 "2010-06-09T15:20:00Z"

For hassle free date manipulation I like to use Datejs - An open-source JavaScript Date Library, it does a lot of great things with dates, the main cost of using it thought is the 26kb filesize.

It will parse "2010-06-09T15:20:00Z"

冷弦 2024-12-06 14:54:52
Date.fromISO= (function(){
    var diso= Date.parse('2011-04-26T13:16:50Z');
    if(diso=== 1303823810000) return function(s){
        return new Date(Date.parse(s));
    }
    else return 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/).map(function(itm){
                return parseInt(itm, 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;
    }
})()
Date.fromISO= (function(){
    var diso= Date.parse('2011-04-26T13:16:50Z');
    if(diso=== 1303823810000) return function(s){
        return new Date(Date.parse(s));
    }
    else return 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/).map(function(itm){
                return parseInt(itm, 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;
    }
})()
相守太难 2024-12-06 14:54:52

我对扩展 Javascript 核心对象的想法感到畏缩,所以我倾向于使用它们。全部归功于Paul Sowden

    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
        "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
        "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    var d = string.match(new RegExp(regexp));

    var offset = 0;
    var time;
    var date = new Date(d[1], 0, 1);

    if (d[3]) { date.setMonth(d[3] - 1); }
    if (d[5]) { date.setDate(d[5]); }
    if (d[7]) { date.setHours(d[7]); }
    if (d[8]) { date.setMinutes(d[8]); }
    if (d[10]) { date.setSeconds(d[10]); }
    if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
    if (d[14]) {
        offset = (Number(d[16]) * 60) + Number(d[17]);
        offset *= ((d[15] == '-') ? 1 : -1);
    }

    offset -= date.getTimezoneOffset();
    time = (Number(date) + (offset * 60 * 1000));
    date.setTime(Number(time));

I cringe at the thought of extending Javascript core objects so I tend to work with them instead. Full credit to Paul Sowden

    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
        "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
        "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    var d = string.match(new RegExp(regexp));

    var offset = 0;
    var time;
    var date = new Date(d[1], 0, 1);

    if (d[3]) { date.setMonth(d[3] - 1); }
    if (d[5]) { date.setDate(d[5]); }
    if (d[7]) { date.setHours(d[7]); }
    if (d[8]) { date.setMinutes(d[8]); }
    if (d[10]) { date.setSeconds(d[10]); }
    if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
    if (d[14]) {
        offset = (Number(d[16]) * 60) + Number(d[17]);
        offset *= ((d[15] == '-') ? 1 : -1);
    }

    offset -= date.getTimezoneOffset();
    time = (Number(date) + (offset * 60 * 1000));
    date.setTime(Number(time));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文