Javascript Date(dateString) 在特定服务器和浏览器上返回 NaN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8098963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Date(dateString) returns NaN on specific server and browser
提问by Gagege
I'm using the Javascript Date(string) constructor with a date format of "yyyy-mm-dd". The constructor works just fine in IE 9 and Firefox unless the app is running on our testing VM which is running IIS. If it's on the VM, in IE 9 it returns 'NaN', but still works normally in Firefox.
我正在使用日期格式为“yyyy-mm-dd”的 Javascript Date(string) 构造函数。构造函数在 IE 9 和 Firefox 中工作得很好,除非应用程序在我们运行 IIS 的测试 VM 上运行。如果它在 VM 上,在 IE 9 中它返回“NaN”,但在 Firefox 中仍然正常工作。
var dateAsString = "2011-11-09";
var dateCreated = new Date(dateAsString);
I was under the assumption that the server had nothing to do with client-side Javascript. Any suggestions?
我假设服务器与客户端 Javascript 无关。有什么建议?
采纳答案by James Hill
I suggest attempting a more reliable form of date parsing. The example below uses setFullYear()
. Does IE produce a different result with the code below?
我建议尝试一种更可靠的日期解析形式。下面的示例使用setFullYear()
. IE 是否会使用以下代码产生不同的结果?
/**Parses string formatted as YYYY-MM-DD to a Date object.
* If the supplied string does not match the format, an
* invalid Date (value NaN) is returned.
* @param {string} dateStringInRange format YYYY-MM-DD, with year in
* range of 0000-9999, inclusive.
* @return {Date} Date object representing the string.
*/
function parseISO8601(dateStringInRange) {
var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
date = new Date(NaN), month,
parts = isoExp.exec(dateStringInRange);
if(parts) {
month = +parts[2];
date.setFullYear(parts[1], month - 1, parts[3]);
if(month != date.getMonth() + 1) {
date.setTime(NaN);
}
}
return date;
}
回答by Wytze
And for those of us who want to know howto replace hyphens (aka dashes) with slashes:
对于我们这些想知道如何用斜杠替换连字符(又名破折号)的人:
new Date(dashToSlash(string));
That uses this function:
使用这个函数:
function dashToSlash(string){
var response = string.replace(/-/g,"/");
//The slash-g bit says: do this more than once
return response;
}
In my case it's much easier to convert hyphens to slashes selectively (only where it's needed for the Date() function) than to replace the date format everywhere in my code.
在我的情况下,有选择地将连字符转换为斜杠(仅在 Date() 函数需要的地方)比在我的代码中替换日期格式要容易得多。
Note: you really need to define a separate 'response' variable and assign it the value of the replace operation result. If you don't, the string is returned unaltered in Chrome. That's not a huge problem, since Chrome doesn't have a problem with hyphenated date strings to begin with. But still...
注意:您确实需要定义一个单独的“响应”变量并为其分配替换操作结果的值。如果您不这样做,则该字符串将在 Chrome 中原封不动地返回。这不是一个大问题,因为 Chrome 一开始就没有带连字符的日期字符串的问题。但是还是...
回答by Lee Kowalkowski
Just use slashes instead of hyphens if you can.
如果可以,只需使用斜杠而不是连字符。
EDIT: Expanded clarification...
编辑:扩大澄清...
The ISO 8601standard format uses the hyphen as a date separator. My answer does not mean you do not need to follow standards. You can use slashes only for the Date constructor if necessary.
在ISO 8601标准格式使用连字符作为日期分隔符。我的回答并不意味着您不需要遵循标准。如有必要,您可以仅对 Date 构造函数使用斜杠。
回答by James Johnson
It's because of the date format. For some reason, IE and Safari get tripped up with yyyy-mm-dd
. Use another date format and you should be all set.
这是因为日期格式。出于某种原因,IE 和 Safari 被yyyy-mm-dd
. 使用另一种日期格式,您应该已准备就绪。
It's talked about here:
http://biostall.com/javascript-new-date-returning-nan-in-ie-or-invalid-date-in-safari
它在这里讨论:http:
//biostall.com/javascript-new-date-returning-nan-in-ie-or-invalid-date-in-safari