javascript - MongoDB 添加月份到日期

标签 javascript mongodb

所以,这让我抓狂,我不确定这是一个错误还是我遗漏了一些明显的东西。有人可以向我解释为什么在 mongo 2.0.2 中的以下语句中添加了一个月

# mongo
MongoDB shell version: 2.0.2
> new Date()
ISODate("2012-02-19T04:58:56.988Z")
> new Date(2012, 02, 19, 04, 58, 56)
ISODate("2012-03-19T04:58:56Z")
> new Date(2012, 01, 19, 04, 58, 56)
ISODate("2012-02-19T04:58:56Z")

注意当我指定日期时如何添加一个月。威士忌探戈狐步舞?

最佳答案

MongoDB 使用 JavaScript 作为其界面语言。 JavaScript Date 构造函数中的 month 是从零开始的。来自fine manual :

month
Integer value representing the month, beginning with 0 for January to 11 for December.

请注意 1 月份的 0。这类事情是 MongoDB 倾向于使用自己的 ISODate function 的原因之一。而不是新日期:

[...] ISODate is a thin wrapper around the Date constructor to fix some of it's shortcomings. It returns a normal Date object with all of the normal methods that javascript Date methods support.

关于javascript - MongoDB 添加月份到日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9346875/

相关文章:

javascript - 在不同时间重新加载多个 DIV

javascript - AngularJS 有没有办法在引导后动态注册指令

json - 如何在 mongodb 中展平子文档

mongodb - 根据 bool 值进行编辑或创建

javascript - 在 AngularJs 中使用 socket.io 进行用户更新时出现错误

javascript - 自定义字符串而不是 console.log 中的 "Object"

javascript - Angular 5 在使用 httpClient 发布之前将 token 添加到 header

mongodb - 如何在云运行上为 docker 容器关联持久卷

Mongodb选择片键

c# - 反序列化 BSON ReadBsonType 只能在 State 为 Type 时调用