猫鼬将存储的UTC日期转换为本地时间? [英] Mongoose converting stored UTC dates to local time?

查看:63
本文介绍了猫鼬将存储的UTC日期转换为本地时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否正常,还是在架构设置或查询过程中缺少某些内容:

I'm wondering if this is normal, or if I'm missing something in the schema setup or query process:

我的应用程序和mongoose在mongodb中正确地将日期存储为UTC.通过mongo shell查看文档可以确认这一点.当我通过mongoose从mongodb中检索文档时,日期现在是本地时间.

My app, and mongoose, is correctly storing a date as UTC in mongodb. This is confirmed by viewing the documents via the mongo shell. When I retrieve the documents from mongodb via mongoose the date is now local time.

有没有办法让猫鼬在查询时将日期保留为UTC?

Is there a way to have mongoose keep the date as UTC when queried?

推荐答案

猫鼬和node.js对您的日期没有任何作用,只是当您调用Date类型会生成本地时间字符串>,即使它实际上包含UTC时间.

Mongoose and node.js aren't doing anything to your dates, it's simply that the JavaScript Date type produces a local time string when you call toString() on it even though it actually contains the time in UTC.

如果要使用UTC时间字符串,请在Date对象上明确调用toUTCString().

Explicitly call toUTCString() on your Date object if you want a UTC time string.

这篇关于猫鼬将存储的UTC日期转换为本地时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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