A
A
Anatoly2017-08-25 18:28:03
MongoDB
Anatoly, 2017-08-25 18:28:03

How to display time correctly from MongoDB?

Hello!
Can someone tell me:
there are records in MongoDB, in the mongo console, I execute the command: db.logs.find()
records are displayed with the correct date / time:
{ "_id" : ObjectId("59a01f6f9508d72ddcb4f67c"), "created" : ISODate(" 2017-08-25T16:00:31.618Z"), "__v" : 0 }
{ "_id" : ObjectId("59a01e399508d72ddcb4f67b"), "created" : ISODate("2017-08-25T15:55:21.234Z") , "__v" : 0 }
{ "_id" : ObjectId("59a01dd19508d72ddcb4f67a"), "created" : ISODate("2017-08-25T15:53:37.579Z"), "__v" : 0
} time.
And when I process the display of this data in the Node.JS script:
Log. find({}).sort({created: -1}).then(data => res.json(data));
then another time is displayed in the browser:
[{"created":"2017-08-25T13:00:31.618Z","id":"59a01f6f9508d72ddcb4f67c"},
{"created":"2017-08-25T12:55:21.234Z","id": "59a01e399508d72ddcb4f67b"},
{"created":"2017-08-25T12:53:37.579Z","id":"59a01dd19508d72ddcb4f67a"}]
-3 hours difference.
I am sure that this is related to time zones, but I don’t understand how to set it up correctly, what needs to be added to Node.JS so that it does not convert this time, but gives it as it is.
I already thought I could connect momentjs, as many people on the network use to work with timezones, but where to register it so that it catches the output between MongoDB and Node.JS
Can anyone tell me what needs to be done?
PS

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-08-25
@boratsagdiev

Maybe this?
https://docs.mongodb.com/manual/reference/method/Date/
Return Date as Date Object
The mongo shell wrap objects of Date type with the ISODate helper; however, the objects remain of type Date.
The following example uses new Date() to return Date object with the specified UTC datetime.
moment.js you can process the dates after receiving from the database, depending on where you need to display these correct dates. It is possible and on the client.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question