蒙戈错误的日期

发布于 2024-12-23 05:47:26 字数 363 浏览 1 评论 0原文

为什么 mongoDB 为我创建了错误的日期。

> start=new Date(2011,12,23)     
"Mon Jan 23 2012 00:00:00 GMT+0000 (UTC)"
> start=new Date(2011,12,24)
"Tue Jan 24 2012 00:00:00 GMT+0000 (UTC)"
> start=new Date(2011,13,24)
"Fri Feb 24 2012 00:00:00 GMT+0000 (UTC)"
> start=new Date(2011,11,24)
"Sat Dec 24 2011 00:00:00 GMT+0000 (UTC)"
> 

Why is mongoDB creating wrong dates for me.

> start=new Date(2011,12,23)     
"Mon Jan 23 2012 00:00:00 GMT+0000 (UTC)"
> start=new Date(2011,12,24)
"Tue Jan 24 2012 00:00:00 GMT+0000 (UTC)"
> start=new Date(2011,13,24)
"Fri Feb 24 2012 00:00:00 GMT+0000 (UTC)"
> start=new Date(2011,11,24)
"Sat Dec 24 2011 00:00:00 GMT+0000 (UTC)"
> 

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

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

发布评论

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

评论(1

南…巷孤猫 2024-12-30 05:47:26

Mongo的日期对象是Javascript日期对象,它的月份是从0开始的(Jan = 0,Dec = 11)。

2011/12/23 is actually month #1 (12-11) of year 2011+1 -> Jan 23/2012
2011/12/24 is actually month #1 (12-11) of year 2011+1 -> Jan 24/2012
2011/13/24 is actually month #2 (13-11) of year 2011+1 -> Feb 24/2012
etc...

除此之外,日历中没有 13 月,因此至少在第三种情况下,您一开始就输入了无效日期 - 只是 JS 规范比较礼貌,并将其翻译为“明年”你认为它完全有效。

Mongo's date object is the Javascript date object, and its months are 0-based (Jan = 0, Dec = 11).

2011/12/23 is actually month #1 (12-11) of year 2011+1 -> Jan 23/2012
2011/12/24 is actually month #1 (12-11) of year 2011+1 -> Jan 24/2012
2011/13/24 is actually month #2 (13-11) of year 2011+1 -> Feb 24/2012
etc...

and besides the point, there is no month 13 in the calendar, so in at least the 3rd case, you're feeding an invalid date in to begin with - it's only the JS spec is polite and tranlates that into "next year" for you that it works at all.

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