Webkit 日期解析

发布于 2024-11-26 18:10:06 字数 916 浏览 6 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百变从容 2024-12-03 18:10:06

我认为 Date.parse 方法不一致(实际格式因浏览器、JS 引擎版本等而异)。我习惯于使用以下方法在 JavaScript 中对日期进行各种操作(例如比较):

  1. 以某种方式获取日期的组成部分(在您的情况下,您只需用“-”分隔字符串)。
  2. 使用上一步中的组件构建日期。
  3. 使用 getTime() 方法比较日期的 unix 时间戳。

这是代码:

var d1Parts = s1.split('-'),
    d2Parts = s2.split('-'),
    d1 = new Date(d1Parts[0], d1Parts[1]-1, d1Parts[2]),
    d2 = new Date(d2Parts[0], d2Parts[1]-1, d2Parts[2]);

if ( d1.getTime() < d2.getTime() ) {
    console.log('the first date is before the second');
}

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):

  1. Get the components of the date somehow (in your case you can just split the string by '-').
  2. Construct the date using components from the previous step.
  3. Compare the unix timstamp of the dates using getTime() method.

Here is the code:

var d1Parts = s1.split('-'),
    d2Parts = s2.split('-'),
    d1 = new Date(d1Parts[0], d1Parts[1]-1, d1Parts[2]),
    d2 = new Date(d2Parts[0], d2Parts[1]-1, d2Parts[2]);

if ( d1.getTime() < d2.getTime() ) {
    console.log('the first date is before the second');
}
猫弦 2024-12-03 18:10:06

这个小函数修复了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文