【问题标题】:how to change date timezone in mongoose?如何更改猫鼬中的日期时区?
【发布时间】:2016-06-10 21:07:53
【问题描述】:

在模型架构中,

使用

updated: {
    type: Date,
    default: Date.now

在 server.js 中

put(function(req, res) {
    var query = {name: req.params.name};
    // use our bear model to find the bear we want
    Domain.find(query, function(err, domains) {

        if (err)
            res.send(err);
        var domain = domains[0];
        domain.password = req.body.password;  // update the bears info
        domain.updated = new Date();
        // save the bear
        domain.save(function(err, data) {
            if (err)
                res.send(err);

            res.json({ status: 'success', message: 'domain updated!' }, data);
        });

    });
});

然而,

在 db 端显示,

"updated": "2016-02-27T16:20:42.941Z"

但是,我的时区是 UTC+02.00

所以应该是 18:20:42

我做错了什么?

【问题讨论】:

    标签: node.js date express mongoose


    【解决方案1】:

    我正在使用时刻时区

    npm install moment-timezone
    
    const moment = require('moment-timezone');
    const dateThailand = moment.tz(Date.now(), "Asia/Bangkok");
    
    console.log(dateThailand); // "2018-08-20T16:35:14.033+07:00"
    *** Asia/Bangkok +07:00
    

    猫鼬中的架构。

    const categorySchema = new Schema(
        {
            _id: {type: mongoose.Schema.Types.ObjectId, auto: true},
            c_name: String,
            created_by: String,
            created_date: {type: Date, default: dateThailand},
            updated_by: String,
            updated_date: {type: Date, default: dateThailand}
        }, {_id: false}
    );
    

    看到created_date, updated_date: {type: Date, default: dateThailand }

    阅读更多:http://momentjs.com/timezone/docs/

    *如果您使用 Robo 3T 工具。

    您可以设置“显示日期...”

    Options > Display Dates In... > Local Timezone
    

    :) 为我工作。

    【讨论】:

      【解决方案2】:

      时间戳与时区无关,存储为 unix 时间戳。此时间戳将跨时区工作,节点使用服务器的当前时区解释它。您显示的日期已正确存储。一旦您检索到它,如果您的服务器的时区是 UTC+2,它将显示正确的时间。

      【讨论】:

      • 我的 ubuntu 服务器时区是 EET,所以我需要将其更改为 UTC+2 吗?
      • 最佳做法是将服务器的时区保持为 UTC,这样您就不会因日期时间差异查询而感到沮丧。日期时间将更容易推断出 UTC 时间。我已经做过很多次了,它让你免于很多时区转换的麻烦。
      【解决方案3】:

      您的代码没有任何问题。无论您尝试在哪个时区插入日期,MongoDb 都会以 UTC 格式保存日期。

      如果您在保存到数据库之前记录 domain.updated,结果将是 UTC+2(您的本地时间)

      如果您在 DB 中看到更新的列,则结果将采用 UTC

      如果您从数据库中获取更新的列值,那么结果将再次为 UTC+2(您的本地时间)

      【讨论】:

      • 那么,我该如何解决呢? Console.log 给出正确的时间,但插入后显示不同的时间!
      【解决方案4】:

      您可以从特定的 UTC 时间创建日期对象:

      new Date(Date.UTC(year, month, day, hour, minute, second))

      【讨论】:

        【解决方案5】:

        我改变了这个,

        var utc = new Date();
        utc.setHours( utc.getHours() + 2);
        domain.updated = utc;
        

        现在可以了。

        【讨论】:

          【解决方案6】:

          使用moment.js 很简单:

          var moment = require('moment');
          
          var utcDate = moment.utc().toDate();
          

          享受吧!

          【讨论】:

            【解决方案7】:

            请记住,无论您在 mongoose 架构中使用什么设置时间,mongoose 都将始终使用 UTC 时间,因此您需要在 Schema 内动态分配 UTC 时间戳。就是这样:-

            var current = new Date();
            const timeStamp = new Date(Date.UTC(current.getFullYear(), 
            current.getMonth(),current.getDate(),current.getHours(), 
            current.getMinutes(),current.getSeconds(), current.getMilliseconds()));
            
            //Here goes your schema 
            const auditSchema = mongoose.Schema({
                       dateTime : { type: Date, default : timeStamp }
            })
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-06-14
              • 2012-02-17
              • 1970-01-01
              • 2017-04-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-05-03
              相关资源
              最近更新 更多