admin管理员组文章数量:1181449
I know that I can set Date.now
as a default value in a mongoose
schema, but what other Javascript date types or are all dates automatically converted to mongodb´s standard format - ISOdate
?
Are there anything I should be concerned about when it comes to storing dates?
dates: {
created: {type: Date, default: Date.now},
}
Thanks
I know that I can set Date.now
as a default value in a mongoose
schema, but what other Javascript date types or are all dates automatically converted to mongodb´s standard format - ISOdate
?
Are there anything I should be concerned about when it comes to storing dates?
dates: {
created: {type: Date, default: Date.now},
}
Thanks
Share Improve this question asked Jan 24, 2012 at 16:42 IndustrialIndustrial 42.7k71 gold badges201 silver badges292 bronze badges3 Answers
Reset to default 18The only thing you should take care of is converting the MongoDB/BSON UTC date to your local timezone (in case you need this).
For more info checkout: http://docs.mongodb.org/manual/reference/bson-types/#document-bson-type-date
Mongoose date casting has a couple of small cases where it differs from
JavaScript's native date parsing.
You might be interested in reading how to work with dates, to better understand casting errors and common pitfalls:
https://mongoosejs.com/docs/tutorials/dates.html.
- Mongoose looks for a valueOf() function on the given object, and calls valueOf() before casting the date. This means Mongoose can cast moment objects to dates automatically.
const moment = require('moment');
const user = new User({
name: 'Jean-Luc Picard',
lastActiveAt: moment.utc('2002-12-09')
});
user.lastActiveAt; // "2002-12-09T00:00:00.000Z"
- By default, if you pass a numeric string to the Date constructor, JavaScript will attempt to convert it to a year.
new Date(1552261496289); // "2019-03-10T23:44:56.289Z"
new Date('1552261496289'); // "Invalid Date"
new Date('2010'); // 2010-01-01T00:00:00.000Z
Mongoose converts numeric strings that contain numbers outside the range of representable dates in JavaScript and converts them to numbers before passing them to the date constructor.
you need cover to new Date(req.body.time).toString()
, req.body.time is a time format month/day/year or month-day-year month before day
本文标签: javascriptDates in mongooseStack Overflow
版权声明:本文标题:javascript - Dates in mongoose? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738166767a2066852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论