如何为 Mongoose 中的日期字段设置正确的时区?
How can I set correct timezone for a date field in Mongoose?
我正在使用 moment 生成时间和日期:
const moment = require('moment-timezone');
const emailModel = require('./api/models/emails');
sentTime=moment().tz('America/Los_Angeles').format();
console.log(sentTime); //console log shows correct time
emailModel.findOneAndUpdate({ _id: emailInfo._id }, {sentTime: sentTime }, { upsert: true },function (err, doc) {
if (err)
console.log(err);
});
这是我正在使用的模式 mongoose :
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const EmailSchema = new Schema({
.
.
.
sentTime: {
type: Date,
trim: true
}
.
.
.
});
问题是:
控制台日志显示正确的时间 2020-01-07T12:23:00-08:00 但猫鼬在数据库中保存了错误的时区:2020-01-07T20:23:01.000 +00:00
目前 Mongodb 的默认行为是:(From the docs)
MongoDB stores times in UTC by default, and will convert any local
time representations into this form.
他们推荐的解决方案(也是正确的)是:
Applications that must operate or report on some unmodified local time
value may store the time zone alongside the UTC timestamp, and compute
the original local time in their application logic.
更新:
由于您已经在使用 moment-timezone
,因此我将采用一种简单的方法:
将 EmailSchema
更改为具有时区字段并在该架构上创建 Mongoose virtual field 以调整时间。
const schemaOpts = { toJSON: { virtuals: true } };
const EmailSchema = new Schema(
{
sentTime: {
type: Date,
trim: true
},
timeZone: {
type: String
}
},
schemaOpts
);
EmailSchema.virtual("adjustedTime").get(function() {
return moment.tz(this.sentTime, this.timeZone).format();
});
//fetching data
const result = await EmailSchema.findOne({}).exec();
console.info("result::", result.toJSON());
//note that if using .lean() for performance which has a caveat on using .toJSON()
这个案例的技巧是在保存之前,你需要添加时间和日期。例如:2021/01/02 ==> 2021/01/02 15:00:00,当然小时总是等于或大于 04:00:00。因为没有时间,日期将为 00:00:00 并且 mongo 会将其转换为默认时区并用 4 减去小时。
我正在使用 moment 生成时间和日期:
const moment = require('moment-timezone');
const emailModel = require('./api/models/emails');
sentTime=moment().tz('America/Los_Angeles').format();
console.log(sentTime); //console log shows correct time
emailModel.findOneAndUpdate({ _id: emailInfo._id }, {sentTime: sentTime }, { upsert: true },function (err, doc) {
if (err)
console.log(err);
});
这是我正在使用的模式 mongoose :
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const EmailSchema = new Schema({
.
.
.
sentTime: {
type: Date,
trim: true
}
.
.
.
});
问题是: 控制台日志显示正确的时间 2020-01-07T12:23:00-08:00 但猫鼬在数据库中保存了错误的时区:2020-01-07T20:23:01.000 +00:00
目前 Mongodb 的默认行为是:(From the docs)
MongoDB stores times in UTC by default, and will convert any local time representations into this form.
他们推荐的解决方案(也是正确的)是:
Applications that must operate or report on some unmodified local time value may store the time zone alongside the UTC timestamp, and compute the original local time in their application logic.
更新:
由于您已经在使用 moment-timezone
,因此我将采用一种简单的方法:
将 EmailSchema
更改为具有时区字段并在该架构上创建 Mongoose virtual field 以调整时间。
const schemaOpts = { toJSON: { virtuals: true } };
const EmailSchema = new Schema(
{
sentTime: {
type: Date,
trim: true
},
timeZone: {
type: String
}
},
schemaOpts
);
EmailSchema.virtual("adjustedTime").get(function() {
return moment.tz(this.sentTime, this.timeZone).format();
});
//fetching data
const result = await EmailSchema.findOne({}).exec();
console.info("result::", result.toJSON());
//note that if using .lean() for performance which has a caveat on using .toJSON()
这个案例的技巧是在保存之前,你需要添加时间和日期。例如:2021/01/02 ==> 2021/01/02 15:00:00,当然小时总是等于或大于 04:00:00。因为没有时间,日期将为 00:00:00 并且 mongo 会将其转换为默认时区并用 4 减去小时。