简体   繁体   English

使用猫鼬在Express中更改日期格式

[英]Change date format in express using mongoose

I stored date in database but whenever i am fetching then its format change 我将日期存储在数据库中,但是每当我获取数据时,其格式就会更改

getting date -      2018-07-31T06:48:01.649Z
stored date in db - 2018-07-31 12:18:01.649

here is my code 这是我的代码

var timee = result.created;

By default, dates are stored in UTC in MongoDB so you are seeing the difference in the time zone of stored and retrieved dates. 默认情况下,日期存储在MongoDB中的UTC中,因此您会看到存储日期和检索日期所在时区的差异。

new Date() returns the current date as a Date object. new Date()返回当前日期作为Date对象。 The mongo shell wraps the Date object with the ISODate helper. mongo shell使用ISODate帮助程序包装Date对象。 The ISODate is in UTC . ISODate使用UTC

You can read the MongoDB specification here . 您可以在此处阅读MongoDB规范。

There are several approaches that you can apply. 您可以采用几种方法。 Either convert the date to UTC timezone before saving it or convert into locale time zone after loading the date. 在保存日期之前将其转换为UTC时区,或者在加载日期后将其转换为语言环境时区。

new Date(ISODate().toString() + 'UTC')

ISODate("2018-08-01T11:39:39Z")

and to replace the T and Z with an empty space using the javascript. 并使用javascript将T和Z替换为空白。

dateString.replace("T", " ").replace("Z", " ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM