如何更改猫鼬的日期时区? [英] how to change date timezone in mongoose?

查看:118
本文介绍了如何更改猫鼬的日期时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模型模式中,

使用

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

在server.js中

In 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

But, my timezone is UTC+02.00

所以应该像18:20:42

So it should be like 18:20:42

我做错了什么?

推荐答案

我正在使用矩时区

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工具.

您可以设置显示日期在..."

You can set "Display Dates In..."

Options > Display Dates In... > Local Timezone

:)为我工作.

这篇关于如何更改猫鼬的日期时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆