MongoDB date in timezone

巧了我就是萌 提交于 2021-01-29 07:02:20

问题


Is there a way to store Dates in local timezone and not in UTC ?

This is my schema :

var WhispSchema = new mongoose.Schema({
    text : String,
    pos : {latitude: Number, longitude: Number},
    distance : {type : Number, default : 0},
    created_by : {type : Schema.Types.ObjectId, ref : "UserSchema"},
    upvote : {type : Number, default : 0},
    downvote : {type : Number, default : 0}
});

WhispSchema.plugin(timestamps, {
  createdAt: 'created_at', 
  updatedAt: 'updated_at'
});

But the field "created_at" and "updated_at" is in UTC format and I want the local timezone.


回答1:


No, the BSON Date data type that's used within MongoDB to store dates is defined as a UTC datetime.

But this is really for the best, as storing datetimes in other time zones can get really messy. It's better to convert the stored UTC timestamps to a local timezone as needed after getting it from the database.

See this question regarding ways to do this within an aggregate pipeline.



来源:https://stackoverflow.com/questions/34135851/mongodb-date-in-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!