2

我有一个格式如下的日期字符串:

2012-09-20T01:36:51.556Z

它是从 mongodb 原样返回的日期字段。Chrome、FF 和 IE 能够解析此字符串,但是 Safari 失败并出现错误Invalid Date。我试过 DateJS,但它也无法解析这个日期。任何想法如何轻松解析此日期?或者,是什么导致 Safari 失败?

我正在为 mongodb 使用本机节点驱动程序。它以上述格式将日期作为字符串返回。

4

3 回答 3

1

我的 DateExtensions 应该解析:

http://depressedpress.com/javascript-extensions/dp_dateextensions/

将库添加到页面后,您将使用静态 Date.parseIso8601( date ) 方法解析日期。

为此,扩展有点繁重(尽管如果您愿意,您可以只提取数据解析的东西)。

我很惊讶 Safari 遇到了这样的问题,因为在我看来,日期是一个完全有效的 ISO 8601 日期(定义如下:http: //www.w3.org/TR/NOTE-datetime)。这与约会时的普通香草差不多。完全猜测,但是您是否尝试过用空格替换“T”分隔符?这不是标准的,但我已经看到很多以这种方式使用它的实现(并且我的组件允许它)。

对于它的价值,这是我从 DP_DateExtensions 中提取的方法 - 我认为它会为你解决问题:

    // parseIso8601
    // Attempts to convert ISO8601 input to a date
Date.parseIso8601 = function(CurDate) {

        // Check the input parameters
    if ( typeof CurDate != "string" || CurDate == "" ) {
        return null;
    };
        // Set the fragment expressions
    var S = "[\\-/:.]";
    var Yr = "((?:1[6-9]|[2-9][0-9])[0-9]{2})";
    var Mo = S + "((?:1[012])|(?:0[1-9])|[1-9])";
    var Dy = S + "((?:3[01])|(?:[12][0-9])|(?:0[1-9])|[1-9])";
    var Hr = "(2[0-4]|[01]?[0-9])";
    var Mn = S + "([0-5]?[0-9])";
    var Sd = "(?:" + S + "([0-5]?[0-9])(?:[.,]([0-9]+))?)?";
    var TZ = "(?:(Z)|(?:([\+\-])(1[012]|[0]?[0-9])(?::?([0-5]?[0-9]))?))?";
        // RegEx the input
        // First check: Just date parts (month and day are optional)
        // Second check: Full date plus time (seconds, milliseconds and TimeZone info are optional)
    var TF;
    if ( TF = new RegExp("^" + Yr + "(?:" + Mo + "(?:" + Dy + ")?)?" + "$").exec(CurDate) ) {} else if ( TF = new RegExp("^" + Yr + Mo + Dy + "[Tt ]" + Hr + Mn + Sd + TZ + "$").exec(CurDate) ) {};
        // If the date couldn't be parsed, return null
    if ( !TF ) { return null };
        // Default the Time Fragments if they're not present
    if ( !TF[2] ) { TF[2] = 1 } else { TF[2] = TF[2] - 1 };
    if ( !TF[3] ) { TF[3] = 1 };
    if ( !TF[4] ) { TF[4] = 0 };
    if ( !TF[5] ) { TF[5] = 0 };
    if ( !TF[6] ) { TF[6] = 0 };
    if ( !TF[7] ) { TF[7] = 0 };
    if ( !TF[8] ) { TF[8] = null };
    if ( TF[9] != "-" && TF[9] != "+" ) { TF[9] = null };
    if ( !TF[10] ) { TF[10] = 0 } else { TF[10] = TF[9] + TF[10] };
    if ( !TF[11] ) { TF[11] = 0 } else { TF[11] = TF[9] + TF[11] };
        // If there's no timezone info the data is local time
    if ( !TF[8] && !TF[9] ) {
        return new Date(TF[1], TF[2], TF[3], TF[4], TF[5], TF[6], TF[7]);
    };
        // If the UTC indicator is set the date is UTC
    if ( TF[8] == "Z" ) {
        return new Date(Date.UTC(TF[1], TF[2], TF[3], TF[4], TF[5], TF[6], TF[7]));
    };
        // If the date has a timezone offset
    if ( TF[9] == "-" || TF[9] == "+" ) {
            // Get current Timezone information
        var CurTZ = new Date().getTimezoneOffset();
        var CurTZh = TF[10] - ((CurTZ >= 0 ? "-" : "+") + Math.floor(Math.abs(CurTZ) / 60))
        var CurTZm = TF[11] - ((CurTZ >= 0 ? "-" : "+") + (Math.abs(CurTZ) % 60))
            // Return the date
        return new Date(TF[1], TF[2], TF[3], TF[4] - CurTZh, TF[5] - CurTZm, TF[6], TF[7]);
    };
        // If we've reached here we couldn't deal with the input, return null
    return null;

};

我相信您可以创建一个更小、更精简的版本(我的代码是为了可维护性而不是紧凑性而编写的)——但这将管理 ISO8601 日期时间的大多数变化,并且从未让我失望。;^)

希望这可以帮助!

于 2012-09-20T02:41:22.713 回答
0

我最终将日期转换为服务器端的时间戳,然后将其发送给客户端。NodeJS(当然是基于 Chrome 的 JavaScript 引擎)可以很好地解析这种格式。

于 2012-10-08T01:28:32.240 回答
0

我有同样的问题,这对我有用:https ://github.com/csnover/js-iso8601

它覆盖了 Date.parse 函数。

于 2013-01-23T16:09:57.483 回答