node.js 如何在猫鼬中更改日期时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35672248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change date timezone in mongoose?
提问by Aras Serdaro?lu
In model schema,
在模型模式中,
Using
使用
updated: {
type: Date,
default: Date.now
In server.js
在 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);
});
});
});
However,
然而,
In db side it shows,
在数据库端它显示,
"updated": "2016-02-27T16:20:42.941Z"
But, my timezone is UTC+02.00
但是,我的时区是 UTC+02.00
So it should be like 18:20:42
所以应该是 18:20:42
What I'm doing wrong?
我做错了什么?
采纳答案by bamossza
I'm using moment-timezone
我正在使用时刻时区
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
Schema in the mongoose.
猫鼬中的架构。
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}
);
See up that created_date, updated_date: {type: Date, default: dateThailand }
看到那个 created_date, updated_date: {type: Date, default: dateThailand }
Read more:http://momentjs.com/timezone/docs/
阅读更多:http : //momentjs.com/timezone/docs/
*If you using Robo 3T tool.
*如果您使用 Robo 3T 工具。
You can set "Display Dates In..."
您可以设置“显示日期在...”
Options > Display Dates In... > Local Timezone
:) Work for me.
:) 对我来说有效。
回答by Tushar Arora
The timestamps are timezone agnostic, stored as a unix timestamp. This timestamp will work across timezones, and node interprets it using current timezone of the server. The date you've shown is correctly stored. As soon as you'll retrieve it, if your server's timezone is UTC+2, it will show you correct time.
时间戳与时区无关,存储为 unix 时间戳。此时间戳将跨时区工作,节点使用服务器的当前时区对其进行解释。您显示的日期已正确存储。只要您检索它,如果您的服务器的时区是 UTC+2,它就会显示正确的时间。
回答by node_saini
There is nothing wrong in your code. MongoDb saves date in UTC format no matter in whichever timezone you try to insert your date.
您的代码没有任何问题。无论您尝试在哪个时区插入日期,MongoDb 都以 UTC 格式保存日期。
If you log domain.updated before saving in DB, result will be UTC+2 (your local time)
如果您在保存到数据库之前登录 domain.updated,结果将是 UTC+2(您的本地时间)
If you see updated column in DB, result will be in UTC
如果您在 DB 中看到更新的列,则结果将采用 UTC
If you fetch updated column value from DB, then again result will be in UTC+2 (your local time)
如果您从数据库中获取更新的列值,那么结果将再次使用 UTC+2(您的本地时间)
回答by Eric Gopak
回答by emilioriosvz
You can create a Date Object from a specific UTC time:
您可以从特定的 UTC 时间创建日期对象:
new Date(Date.UTC(year, month, day, hour, minute, second))
回答by Aras Serdaro?lu
I changed this,
我改了这个
var utc = new Date();
utc.setHours( utc.getHours() + 2);
domain.updated = utc;
Now it works.
现在它起作用了。


