如何更改猫鼬中的日期时区?

新手上路,请多包涵

在模型模式中,

使用

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);
        });

    });
});

然而,

在分贝侧它显示,

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

但是,我的时区是 UTC+02.00

所以应该是 18:20:42

我做错了什么?

原文由 Aras Serdaroğlu 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 686
2 个回答

我正在使用时刻时区

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

在此处输入图像描述

:) 为我工作。

原文由 bamossza 发布,翻译遵循 CC BY-SA 4.0 许可协议

时间戳与时区无关,存储为 unix 时间戳。这个时间戳将跨时区工作,节点使用服务器的当前时区来解释它。您显示的日期已正确存储。只要你检索它,如果你的服务器的时区是 UTC+2,它就会显示正确的时间。

原文由 Tushar Arora 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题