Webkit 日期解析
可能的重复:
Safari JS 无法解析 YYYY-MM-DD 日期格式?
我有一个简单的函数来计算日期之间的天数。
function checkDias(){
str1 = $('#salida').val();
str2 = $('#regreso').val();
difference = Math.floor(( Date.parse(str2) - Date.parse(str1) ) / 86400000);
if (diferencia > 0) { } else {
console.log(difference); console.log(str1); console.log(str2);
// alert the user....
}
}
我这样做是为了检查第二个日期是否在第一个日期之后。在 Firefox 中可以正常工作,但在 Webkit(Safari 或 Chrome)中则不行。 str1 和 str2 可以正常登录控制台,但区别是 Webkit 中的 var 返回 NaN,而 FF 中则返回 int 值。
知道为什么会这样吗?我正在使用 jQuery UI 日期小部件来获取 YYYY-MM-DD 格式,但我认为这与它没有任何关系。
谢谢!
Possible Duplicate:
Safari JS cannot parse YYYY-MM-DD date format?
I have a simple function to calculate the amount of days between to dates.
function checkDias(){
str1 = $('#salida').val();
str2 = $('#regreso').val();
difference = Math.floor(( Date.parse(str2) - Date.parse(str1) ) / 86400000);
if (diferencia > 0) { } else {
console.log(difference); console.log(str1); console.log(str2);
// alert the user....
}
}
I do this to check that the 2nd date is after the first one. In Firefox this works ok but in Webkit (Safari or Chrome) it doesn't. str1 and str2 log into the console just fine but the difference var in Webkit return NaN and in FF it returns the int value.
Any idea why this might be? I'm using a jQuery UI date widget to get and YYYY-MM-DD format but I don't think this has to do anything with it.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 Date.parse 方法不一致(实际格式因浏览器、JS 引擎版本等而异)。我习惯于使用以下方法在 JavaScript 中对日期进行各种操作(例如比较):
这是代码:
The Date.parse method is inconsistent in my opinion (the actual format differs across various browsers, versions of JS engine etc.). I'm used to the following method to make various operations on dates in JavaScript (compare for example):
Here is the code:
这个小函数修复了 Webkit 和 IE 的 Date.parse:
https://github.com/csnover/js-iso8601
我测试了它,现在 Webkit 和 IE 按预期解析日期。
This small function fixes Date.parse for Webkit and IE:
https://github.com/csnover/js-iso8601
I tested it and now Webkit and IE parse the dates as expected.