JavaScript 日期到时间戳计算不适用于任何月份的 8 号和 9 号

发布于 2024-12-01 14:04:34 字数 198 浏览 3 评论 0原文

我有以下 JavaScript: http://jsfiddle.net/5Hapw/

选择日期时,变量将提醒说出时间戳 - 然而,在任何一个月的 8 号和 9 号,时间戳总是错误的。

谁能看到我哪里出了问题?

提前致谢。

I have the following JavaScript: http://jsfiddle.net/5Hapw/

When selecting a date, a variable will alert to say the timestamp - however, on the 8th and 9th of any month, the timestamp is always wrong.

Can anyone see where I have gone wrong?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

笛声青案梦长安 2024-12-08 14:04:34

您的问题是将字符串解析为数字。如果不指定基数,并且字符串以零开头,它将被解析为八进制数:

parseInt("017") === 15

parseInt("08") === 0 // because 8 is not a valid digit in octal numbers

解决方案是指定基数 10:

parseInt("08", 10) === 8

更新的小提琴: http://jsfiddle.net/Guffa/5Hapw/6/

Your problem is in parsing a string into a number. If you don't specify a radix, and the string starts with a zero, it will be parsed as an octal number:

parseInt("017") === 15

parseInt("08") === 0 // because 8 is not a valid digit in octal numbers

The solution is to specify the radix 10:

parseInt("08", 10) === 8

Updated fiddle: http://jsfiddle.net/Guffa/5Hapw/6/

猥琐帝 2024-12-08 14:04:34

如果您不将第二个参数传递给 parseInt (基本),JavaScript 会猜测它。估计是猜错了。将 10 作为第二个参数传递给所有 parseInt 调用(您可以删除从月份中截断零的代码 - 顺便添加此代码以从月份中的某一天截断零也可以工作)

更新的小提琴: http://jsfiddle.net/5Hapw/4/

If you don't pass a second parameter to parseInt (the base) javascript will guess at it. It's probably guessing wrong. Pass 10 in as the second param to all your parseInt calls (you can get rid of the code that truncates the zero from the month - incidentally adding this code to truncate the zero from the day of month would also work)

Updated fiddle: http://jsfiddle.net/5Hapw/4/

┈┾☆殇 2024-12-08 14:04:34

在您的情况下,可以通过将其转换为数字来解决此问题 - 将 parseInt(selection[0]) 替换为 Number(selection[0])

http://jsfiddle.net/mblase75/5Hapw/5/

This problem can be solved in your case by casting it as a number -- replace parseInt(selection[0]) with Number(selection[0]).

http://jsfiddle.net/mblase75/5Hapw/5/

迷荒 2024-12-08 14:04:34

实际上,JavaScript 将这些变量解释为八进制值,请使用将它们转换为十进制。我相信您正在获取一个字符串并将其解析为一个值。像这样的事情。那么请执行以下操作。

parseInt(month_val);

这是我用来检查有效日期的代码片段。

    var birthdate = element.value.split("-");
    var JDate = new Date(parseInt(birthdate[2],10), parseInt(birthdate[0],10) -1, parseInt(birthdate[1],10));
    valid = (parseInt(birthdate[0],10) - 1 == JDate.getMonth() && parseInt(birthdate[1],10) == JDate.getDate() && parseInt(birthdate[2],10) == JDate.getFullYear());
    return valid;

Actually javascript interpret those variable in octal values please convert them into decimal by using. I believe you are taking a string and parsing it for a value. some thing like this. Then please do the following thing.

parseInt(month_val);

And here is my snippet f code which I'm using to check the valid date.

    var birthdate = element.value.split("-");
    var JDate = new Date(parseInt(birthdate[2],10), parseInt(birthdate[0],10) -1, parseInt(birthdate[1],10));
    valid = (parseInt(birthdate[0],10) - 1 == JDate.getMonth() && parseInt(birthdate[1],10) == JDate.getDate() && parseInt(birthdate[2],10) == JDate.getFullYear());
    return valid;
秋日私语 2024-12-08 14:04:34

我不知道你为什么尝试解析 date 参数(通过拆分)?参数 inst 包含以下字段:

  • inst.selectedDay
  • inst.selectedMonth
  • inst.selectedYear
  • 以及更多...

    Date.UTC(inst.selectedYear,inst.selectedMonth,inst.selectedDay);

I dont known why you try parse date parameter (by spliting)? Parameter inst contain next fields:

  • inst.selectedDay
  • inst.selectedMonth
  • inst.selectedYear
  • and more ...

    Date.UTC(inst.selectedYear,inst.selectedMonth,inst.selectedDay);

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