为什么这个月份不同

发布于 2024-09-12 01:49:43 字数 330 浏览 4 评论 0原文

下面的代码:

var unixDate = new Date('07/28/2010');
    var unixMonth = unixDate.getMonth();
    var unixDay = unixDate.getDate();
    var unixYear = unixDate.getFullYear();
    alert(filterDate.value);
    alert(unixMonth);
    alert(unixDay);
    alert(unixYear);

应该给我 07 月,但它提醒 06.... 为什么会这样?

The following code below:

var unixDate = new Date('07/28/2010');
    var unixMonth = unixDate.getMonth();
    var unixDay = unixDate.getDate();
    var unixYear = unixDate.getFullYear();
    alert(filterDate.value);
    alert(unixMonth);
    alert(unixDay);
    alert(unixYear);

should give me month 07 but it alerts 06.... why's that?

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

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

发布评论

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

评论(5

薄荷→糖丶微凉 2024-09-19 01:49:44

月份是从零开始的。只需执行+1即可。另请参阅 MDC 的 Date.getMonth()

getMonth 返回的值是 0 到 11 之间的整数。0 对应一月,1 对应二月,依此类推。

Months are zero based. Just do +1. See also Date.getMonth() at MDC:

The value returned by getMonth is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.

凉墨 2024-09-19 01:49:44

.getMonth 返回零索引月份。因此,0 = 一月,11 = 十二月。

.getMonth returns a zero indexed month. So, 0 = January, and 11 = December.

甜尕妞 2024-09-19 01:49:44

使用:

var unixMonth = unixDate.getMonth() + 1;

.getMonth 返回零索引月份。

0  = January
11 = December

更多信息

getMonth() 方法返回
指定的月份(从 0 到 11)
日期,根据当地时间。

注意:一月为 0,二月为 1,并且
等等。

Use:

var unixMonth = unixDate.getMonth() + 1;

.getMonth returns a zero indexed month.

0  = January
11 = December

More Info

The getMonth() method returns the
month (from 0 to 11) for the specified
date, according to local time.

Note: January is 0, February is 1, and
so on.

风向决定发型 2024-09-19 01:49:44

我的猜测是 0 = 一月,因此您的枚举略有偏差。

My guess would be that 0 = January and thus your enumeration is slightly off.

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