猫鼬以UTC时间保存和检索日期,更改为服务器时区 [英] Mongoose saving and retrieving dates with UTC time, change to server timezone
问题描述
我正在使用猫鼬模型保存我的记录,包括创建和更新的日期.这是我的模型架构:
I'm using mongoose models to save my records including created and updated dates. Here's my model schema:
var CasesModelSchema = new mongoose.Schema(
{
caseId: Number,
sessionId: String,
createdAt: {type: Date},
updatedAt: {type: Date, default: Date.now},
docs: mongoose.Schema.Types.Mixed
},
{
collection: 'cases'
}
);
我面临的问题是updatedAt
字段将日期时间保存为ISODate("2017-04-24T12:40:48.193Z")
,这是UTC,但是我服务器的时区是Asia/Calcutta
.由于我需要根据服务器的时间进行查询,因此需要将日期时间保存在我的首选时区中.
The problem I'm facing is that the updatedAt
field saves the datetime as ISODate("2017-04-24T12:40:48.193Z")
, which is in UTC, but my server's timezone is Asia/Calcutta
. Since I need to make queries according to my server's time, I need the datetimes to be saved in my preferred timezone.
这是我需要执行的查询(获取最近10天的所有数据)
Here's the query I need to execute (get all data for the last 10 days)
var today = moment(moment().format('YYYY-MM-DD')).toDate();
var tenDaysDate = moment(moment().format('YYYY-MM-DD')).add(-10, 'days').toDate();
CasesModel.findOne({updatedAt: {$gte: tenDaysDate, $lt: today}},
function(err, caseData){
cl(caseData, __line);
});
我想要做的是查询以获取最近10天(从10天前午夜(Asia/Calcutta
时区)到今天午夜(Asia/Calcutta
时区)的所有更新记录.我该怎么办?
What I want is to do a query to get all the updated records in the last 10 days, exactly from 10 days ago midnight (Asia/Calcutta
timezone) to today's midnight (Asia/Calcutta
timezone). How do I do that?
推荐答案
我想要做的是查询以获取最近10天(从10天前午夜(亚洲/Calcutta时区)到今天午夜(亚洲/Calcutta时区))的所有更新记录.我该怎么办?
What I want is to do a query to get all the updated records in the last 10 days, exactly from 10 days ago midnight (Asia/Calcutta timezone) to today's midnight (Asia/Calcutta timezone). How do I do that?
如果在今天的午夜"之前表示发生的最后午夜(在这种情况下,您将不会从前一刻获得结果),那么您可以使用一些方法这样-使用您的当地时区:
If by "today's midnight" you mean the last midnight that happened (in which case you will not get results from a moment ago), then you can use something like this - using your local time zone:
// use moment-timezone to handle time zones correctly:
const moment = require('moment-timezone');
let end = moment().startOf('day').tz('UTC')
let start = end.subtract(10, 'days');
或明确使用Asia/Calcutta时区:
or explicitly using Asia/Calcutta time zone:
let end = moment.tz('Asia/Calcutta').startOf('day').tz('UTC');
let start = end.subtract(10, 'days');
如果在今天的午夜"之前表示将要发生的下一个午夜(在这种情况下,您将会从前一刻获得结果),那么您可以使用一些方法这样-使用您的当地时区:
If by "today's midnight" you mean the next midnight that will happen (in which case you will get results from a moment ago), then you can use something like this - using your local time zone:
let end = moment().endOf('day').tz('UTC')
let start = end.subtract(10, 'days');
或明确使用Asia/Calcutta时区:
or explicitly using Asia/Calcutta time zone:
let end = moment.tz('Asia/Calcutta').endOf('day').tz('UTC');
let start = end.subtract(10, 'days');
您将拥有与本地午夜相对应的UTC日期,可以在Mongo查询中使用.
You will have UTC dates that correspond to your local midnights, ready to use in Mongo queries.
无需在Mongo中进行任何更改.
No need to change anything in Mongo.
这篇关于猫鼬以UTC时间保存和检索日期,更改为服务器时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!