【问题标题】:How to do the Time Date UTC to Local and viceversa using MongoDB and Mongoose/Express?如何使用 MongoDB 和 Mongoose/Express 将时间日期 UTC 转换为本地,反之亦然?
【发布时间】:2015-04-18 15:47:10
【问题描述】:

我想根据我的当地时区比较日期(亚洲/加尔各答 GMT+5.30)。

var today = moment().toDate();
var tomorrow = moment().add(1,'days').toDate();

Moment:使用 MomentJS(不确定是否适用于此)

Deal.find({date:{$gte: today, $lt: tomorrow}},function(err, result){
        if(err) return console.log("Error" + err);
    //Code goes here.
});

中的日期

Deal.find({date:{$gte: today, $lt: tomorrow}},function(err, result) {

是格林威治标准时间,而今天和明天是我的本地时区,如上所述。

我该怎么做?我的范围仅限于我当地的时区,并且不关心是 UTC 还是 GMT。

我使用的是标准 Express4、MongoDB、Mongoose ODM 堆栈。

在传递给 Deal.find mongoose 之前,我们有什么方法可以预处理并将 date 变量修改为本地时区询问?

【问题讨论】:

  • date.toISOString() 怎么样
  • 它将转换为字符串对象,Mongoose Query 处理日期对象,对吧?我错过了什么吗?
  • 你说得对,我认为它同时接受,另一种方法是使用 getTimezoneOffset 并使用该偏移量进行查询

标签: javascript mongodb datetime express mongoose


【解决方案1】:

这是一个愚蠢的 Robo Mongo 设置问题。

在 Robo Mongo 窗口中,

  1. 转到选项。
  2. 选择显示日期。
  3. 选择当地时区。

那么你就被排序了!

干杯,谢谢!

【讨论】:

    猜你喜欢
    • 2016-09-20
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 2011-06-27
    • 2013-07-09
    • 1970-01-01
    相关资源
    最近更新 更多