最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node + Mongodb + ISODate + Timezone issue - Stack Overflow

programmeradmin1浏览0评论

Hello I am working in node js + mongodb.

When I insert the data into collection the data is stored default ISODate eg. ISODate("2016-06-17T13:00:21.665Z")

I want to insert the date fields should be store in UTC.

Its already stored in UTC format or need to change anything in code ?

Please anyone suggest me.

My Code:

var query = {};
query.created_by = 'admin';
query.created_on = new Date();
var collection = mongoUtil.list;
collection.insert(query, function(err, item) {
    if (!err && item) {
        console.log("success");
    } else {
        console.log("error");
    }

});

Mongo inserted data:

{ "created_by" : "admin", "created_on" : ISODate("2016-06-17T13:00:21.665Z") }

I checked the refference - Mongo UTC Refference From Document:

  1. Date() returns the current date as a string in the mongo shell.
  2. new Date() returns the current date as a Date object. The mongo shell wraps the Date object with the ISODate helper. The ISODate is in UTC.

Hello I am working in node js + mongodb.

When I insert the data into collection the data is stored default ISODate eg. ISODate("2016-06-17T13:00:21.665Z")

I want to insert the date fields should be store in UTC.

Its already stored in UTC format or need to change anything in code ?

Please anyone suggest me.

My Code:

var query = {};
query.created_by = 'admin';
query.created_on = new Date();
var collection = mongoUtil.list;
collection.insert(query, function(err, item) {
    if (!err && item) {
        console.log("success");
    } else {
        console.log("error");
    }

});

Mongo inserted data:

{ "created_by" : "admin", "created_on" : ISODate("2016-06-17T13:00:21.665Z") }

I checked the refference - Mongo UTC Refference From Document:

  1. Date() returns the current date as a string in the mongo shell.
  2. new Date() returns the current date as a Date object. The mongo shell wraps the Date object with the ISODate helper. The ISODate is in UTC.
Share Improve this question edited Sep 12, 2016 at 8:21 RSKMR asked Sep 12, 2016 at 8:14 RSKMRRSKMR 1,8925 gold badges32 silver badges74 bronze badges 3
  • what format are you getting in node.js? – abdulbari Commented Sep 12, 2016 at 8:48
  • pls tell me , how to check? – RSKMR Commented Sep 12, 2016 at 8:49
  • It's stored in the UTC format in your datastore. If you want to format the date in node.js you could use moments plugin – HoefMeistert Commented Sep 12, 2016 at 9:24
Add a ment  | 

2 Answers 2

Reset to default 2

Yes, as mentioned in abdulbarik's answer, MongoDB does save the Date in UTC.

Date Object:-

You can use getTimezoneOffset() method to get the difference between UTC and Local Time.

  var d = new Date()
  var n = d.getTimezoneOffset();

  console.log('date obj ==>' + d);
  console.log('time zone obj ==>' + n);

Console log:-

You can see the time zone obj has "-60". I am on GMT+1.

date obj ==>Mon Sep 12 2016 10:17:28 GMT+0100 (GMT Daylight Time)
time zone obj ==>-60

MongoDB has persisted the date in UTC:-

I have used object "d" (i.e. new Date()) to persist the date in MongoDB. Though I am on GMT+1, the date is stored in UTC.

 "timestamp" : ISODate("2016-09-12T09:17:28.193Z"),

If you directly insert or update Date, then you will store a timestamp. It will still be stored as the UTC format even it returns as UTC format when you query from Node.js.

But you may or may not want to trust that the time is correct. So for that you can create or use any method to make sure for UTC format.

发布评论

评论列表(0)

  1. 暂无评论