Safari 和 IE 中的 javascript 日期对象问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11253351/
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 object issue in Safari and IE
提问by Fraser
I am taking a date from a JSON object in the format of 2012-12-31 and trying to convert it into friendly values and output it.
我正在从 2012-12-31 格式的 JSON 对象中获取日期,并尝试将其转换为友好值并输出。
var redeemableDate = item.Deal.RedeemableDate; //this is coming in the form of 2012-12-31
var redeemableDate = new Date(redeemableDate);
var rdDay = weekday[redeemableDate.getDay()]; //using an array with weekdays
var rdDate = redeemableDate.getDate();
var rdMonth = monthNames[redeemableDate.getMonth()]; //using an array with month names
var rdYear = redeemableDate.getFullYear();
response.write('Valid ' + rdDay + ' ' + rdDate + ' ' + rdMonth + ' ' + rdYear + ' ONLY');
It all works find and dandy in Firefox and Chrome, but Safari and IE (only tested on IE8 so far) don't like it.
这一切在 Firefox 和 Chrome 中都可以正常工作,但 Safari 和 IE(目前仅在 IE8 上测试过)不喜欢它。
In FF and Chrome I get the expected:
在 FF 和 Chrome 中,我得到了预期的结果:
Valid Sunday 2 September 2012 ONLY
仅在 2012 年 9 月 2 日星期日有效
But in Safari and IE, I get:
但在 Safari 和 IE 中,我得到:
Valid undefined NaN undefined NaN ONLY
有效的 undefined NaN undefined NaN ONLY
When I alert redeemableDate after I have set it as a Date object, Safari returns 'Invalid Date' and IE returns 'NaN'. This is obviously where the issue lies. Is there a way I can get my value into a date object for these browsers?
当我在将其设置为 Date 对象后提醒 RedeemableDate 时,Safari 返回“无效日期”,而 IE 返回“NaN”。这显然是问题所在。有没有办法可以将我的值放入这些浏览器的日期对象中?
回答by Hyman
The yyyy-mm-dd (ISO 8601) date format is not supported in Safari and IE. It is part of ECMAscript 5 though, so it should be just a matter of time.
Safari 和 IE 不支持 yyyy-mm-dd (ISO 8601) 日期格式。不过,它是 ECMAscript 5 的一部分,所以它应该只是时间问题。
A solution would be to pass the date in as arguments to Date.
一种解决方案是将日期作为参数传递给 Date。
var date = "2012-12-31".split("-");
var your_date = new Date(date[0], date[1]-1, date[2]);
Note that month parameter starts at zero (for January), so you must subtract 1 from the value obtained from the string.
请注意,月份参数从零开始(对于一月),因此您必须从从字符串获得的值中减去 1。
EDIT: For a shortcut see answer by joe larson below.
编辑:有关快捷方式,请参阅下面 joe larson 的回答。
回答by Pointy
You're better off parsing the date string yourself:
你最好自己解析日期字符串:
function dateFromISO( str ) {
var d = null;
str.replace(/^(\d\d\d\d)-(\d\d)-(\d\d)$/, function(_, y, m, d) {
d = new Date(parseInt(y, 10), parseInt(m, 10) - 1, parseInt(d, 10));
});
return d;
}
redeemableDate = dateFromISO( redeemableDate );
Even if the other browsers liked those date strings, you'd have the problem of them always being interpreted as UTC. For me, for example, when I pass that string "2012-12-31" to Firefox, it tells me that the date is 30 Dec 2012, because I'm 6 hours behind UTC. In other words, "2012-12-31" is interpreted as midnight of that date, UTC time. Assuming you want everybody in the world to see the right date, if you construct the Date object with numbers it's assumed to be local time at the client.
即使其他浏览器喜欢这些日期字符串,您也会遇到它们总是被解释为 UTC 的问题。例如,对我而言,当我将该字符串“2012-12-31”传递给 Firefox 时,它告诉我日期是 2012 年 12 月 30 日,因为我比 UTC 晚了 6 小时。换句话说,“2012-12-31”被解释为该日期的午夜,UTC 时间。假设您希望世界上的每个人都看到正确的日期,如果您使用数字构造 Date 对象,则假定它是客户端的本地时间。
回答by Kailas
Combining Hymans and joe larson's response, the following code worked well for me:
结合 Hymans 和 joe larson 的回应,以下代码对我来说效果很好:
$scope.formatDate = function(date){
var date = date.split("-").join("/");
var dateOut = new Date(date);
return dateOut;
};
works well for Chrome and Safari... :)
Thanks Hyman and joe larson!!!
适用于 Chrome 和 Safari... :)
谢谢 Hyman 和 joe larson !!!
回答by Brad Koch
Older Internet Explorer versions (and apparently your version of Safari) won't parse a yyyy-mm-dd string, such as new Date('2012-12-31')
.
较旧的 Internet Explorer 版本(显然是您的 Safari 版本)不会解析 yyyy-mm-dd 字符串,例如new Date('2012-12-31')
.
Another option is jQuery.datepicker.parseDate()
, although that may be a bit overkill.
另一种选择是jQuery.datepicker.parseDate()
,尽管这可能有点矫枉过正。
回答by Federico
In the case of using a PHP web service, I would recommend returning your dates using
$row['date'] = date('j/n/Y',strtotime($row['date']));
to have normal functionality with safari/ie.
在使用 PHP Web 服务的情况下,我建议使用$row['date'] = date('j/n/Y',strtotime($row['date']));
safari/ie返回您的日期
以具有正常功能。