IE JavaScript 日期解析错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3020508/
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
IE JavaScript date parsing error
提问by
Why cannot IE parse this string as a Date object.
为什么 IE 不能将此字符串解析为 Date 对象。
var d = Date.parse("Fri Jun 11 04:55:12 +0000 2010"); // returns NaN
However, it works well in FireFox. I am running IE 8.
但是,它在 FireFox 中运行良好。我正在运行 IE 8。
Thanks.
谢谢。
采纳答案by Jitendra
You are getting NaNvalue in IE 8 and its working in Firefox because the format of the string varies with browser and operating system.
您NaN在 IE 8 及其在 Firefox 中的工作中获得了价值,因为字符串的格式因浏览器和操作系统而异。
For example, in IE6 for Windows XP, the string is in the following format:
例如,在 Windows XP 的 IE6 中,字符串采用以下格式:
Tue Dec 05 16:47:20 CDT 2006
But in Firefox for Windows XP, the string is
但是在 Firefox for Windows XP 中,字符串是
Tue Dec 05 2006 16:47:20 GMT-0500
to make it compatible with both browser you will have to first check the browser in your javascript code and then accordingly give your input date string.
要使其与两个浏览器兼容,您必须首先在您的 javascript 代码中检查浏览器,然后相应地提供您的输入日期字符串。
回答by Brian Ellis
I've found the jQuery Globalization Plugindate parsing to work best. Other methods had cross-browser issues and stuff like date.js had not been updated in quite a while.
我发现jQuery Globalization Plugin日期解析效果最好。其他方法存在跨浏览器问题,并且诸如 date.js 之类的东西已经有一段时间没有更新了。
You also don't need a datePicker on the page. You can just call something similar to the example given in the docs:
您也不需要页面上的 datePicker。您可以调用类似于文档中给出的示例的内容:
$.datepicker.parseDate('yy-mm-dd', '2007-01-26');
回答by Luxx
Is solved my problem by creating an date object and let me give it back the timestamp. But for this you need to convert you string into this format:
通过创建日期对象解决了我的问题,并让我将时间戳归还给它。但是为此,您需要将字符串转换为这种格式:
year, month, date, hours, minutes, seconds,ms
an example would be like:
一个例子是:
dateObj = new Date(year, month, date);
timestamp = dateObj.getTime();
This works save in IE and FF.
这在 IE 和 FF 中有效。
IE Dev Center: Date Object (JavaScript)
For your example you would to something like this:
对于您的示例,您将执行以下操作:
//your string
var str = "Fri Jun 11 04:55:12 +0000 2010";
//maps months to integer from 0 to 11
var monthArray = {"Jan":0, "Feb":1, "Mar":2, "Apr":3, "May":4, "Jun":5, "Jul":6, "Aug":7, "Sep":8, "Oct":9, "Nev":10, "Dec":11};
//get the values from the string
var regex = /^[^ ]+ ([^ ]+) (\d{1,2}) (\d{2}):(\d{2}):(\d{2}) \+(\d{4}) (\d{4})$/;
match = regex.exec(str);
var month = monthArray[match[1]],
date = match[2],
hours = match[3],
minutes = match[4],
seconds = match[5],
ms = match[6],
year = match[7];
//create date object with values
var dateObject = new Date(year, month, date, hours, minutes , seconds, ms);
var ts = dateObject.getTime(); //timestamp in ms
回答by Hamza Rashid
Problem
问题
In case your date is stored in SQL datetimelike 2020-04-07 05:30:00and want to parse it in IE. When you parse it with JavaScript in IE using new Date(), it outputs Invalid Datewhile latest versions of Chrome and Firefox parse this date correctly.
如果您的日期存储在 SQL 中datetime,2020-04-07 05:30:00并且想在 IE 中解析它。当您在 IE 中使用 JavaScript 解析它时new Date(),它会输出无效日期,而最新版本的 Chrome 和 Firefox 会正确解析此日期。
Solution
解决方案
You have to replace <space>with Tin datetimestring coming from SQL.
必须更换<space>带有T在datetime字符串从SQL到来。
Example
例子
let myDate = '2020-04-07 05:30:00';
let myFormattedDate = myDate.replace(' ', 'T'); // '2020-04-07T05:30:00'
console.log(new Date(myFormattedDate));
回答by TheBrain
because of the +00000. try to add that the last
因为+00000。尝试添加最后一个
var d = Date.parse("Fri Jun 11 04:55:12 2010 +0000");
回答by superwhatever
This may help you. I just solved a problem similar to this.
这可能对你有帮助。我刚刚解决了一个类似的问题。

