我有一个具有以下格式的日期字符串:
2012-09-20T01:36:51.556Z
它是一个从 mongodb 原样返回的日期字段。 Chrome、FF 和 IE 能够解析此字符串,但是,Safari 失败并显示错误 Invalid Date
。我试过 DateJS,但它也无法解析这个日期。任何想法如何轻松解析此日期?或者,是什么原因导致 Safari 失败?
我正在使用 mongodb 的 native 节点驱动程序。并以上述格式将日期作为字符串返回。
最佳答案
我的 DateExtensions 应该解析:
http://depressedpress.com/javascript-extensions/dp_dateextensions/
将库添加到页面后,您将使用静态 Date.parseIso8601(date) 方法解析日期。
为此,扩展有点繁重(尽管您可以根据需要只提取数据解析内容)。
令我感到惊讶的是,Safari 出现了诸如日期之类的问题,对我来说,它是一个完全有效的 ISO 8601 日期(如此处定义:http://www.w3.org/TR/NOTE-datetime)。这和约会一样普通。完全是猜测,但是您是否尝试过用空格替换“T”分隔符?它不是标准的,但我已经看到很多以这种方式使用它的实现(并且我的组件允许它)。
就其值(value)而言,这是我从 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 日期时间的大多数变化,并且从未让我失望。 ;^)
希望这对您有所帮助!
关于javascript - Safari 中的日期解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12505350/