首页 > 解决方案 > 在猫鼬中使用时间戳时如何更改时区?

问题描述

如何更改猫鼬时间戳中的时区?这里我有我的jobSchema:

模型.js:

const jobSchema = new Schema({
  owner: {
   type: Schema.ObjectId,
   ref: 'User',
   required: true
 },
 title: {
  type: String
 },
 description: {
  type: String
 },
 tag: {
  type: String
 },
 ....
 }, {
  timestamps: true,
  toJSON: {
   virtuals: true,
   transform: (obj, ret) => { delete ret._id }
  },
  toObject: { virtuals: true }
 }) 

时间的结果是:

"createdAt": "2019-01-21T04:35:20.076Z"

但我的实际时间是上午 11.35。

我想将时间戳 TimeZone 更改为 GMT/UTC +7。是否有可能做到这一点?如果可能的话,我该怎么做?谢谢。

标签: node.jsrestmongoosetimezone

解决方案


MongoDB 始终以 UTC 格式存储日期。但是,您可以自己将其转换为您的时区或mongoose-timezone以转换本地时区的日期。


推荐阅读