我正在尝试获取将记录/文档插入MongoDB集合的日期。我正在使用ObjectId.getTimestamp()函数从日期获取时间戳。
该函数返回一个ISODate
对象,然后遍历此question,看起来ISODate
只是本机JavaScript Date
的包装。但是,当我在Date
对象上使用getDay()
和getMonth()
方法时,尽管小时,分钟和年份似乎很好,但返回的结果不正确。
用法(Mongo shell)-
ISODate
据此-
> db.user.count()
1
> db.user.findOne()._id.getTimestamp()
ISODate("2018-10-26T12:52:31Z")
> db.user.findOne()._id.getTimestamp().getDay()
5
> db.user.findOne()._id.getTimestamp().getMonth()
9
> db.user.findOne()._id.getTimestamp().getFullYear()
2018
> db.user.findOne()._id.getTimestamp().getHours()
12
> db.user.findOne()._id.getTimestamp().getMinutes()
52
>
日期是> db.user.findOne()._id.getTimestamp().getDay()
5
> db.user.findOne()._id.getTimestamp().getMonth()
9
,月份是5
,但应该分别是9
和26
。
屏幕截图-
10
我在做什么错?
[返回星期几,但是Date.getDay()返回星期几。
Date.getDate()
对于Date.getMonth(),JavaScript db.user.findOne()._id.getTimestamp().getDate()
26
返回值0-11,即1月0日,2月1日等。10月9日。
Date
LINK db.user.findOne()._id.getTimestamp().getMonth() + 1
10