bendytree
bendytree

Reputation: 13627

Retrieving Dates in MongoJS

I'm trying to store and retrieve a date in mongojs. Here's how it's saved:

var game = { startedOn: new Date() };
db.games.save(game);

When I fetch it, the date is not a date anymore. It's some kind of wrapper around a date. The _d field seems to be a date, but it's weird that I should have to access it that way.

db.games.find(function(err, games){
    console.log(game[0].startedOn);
});

This logs:

{ _useUTC: true,
  _isUTC: true,
  _l: null,
  _i: null,
  _f: null,
  _d: Sun Jun 09 2013 21:49:26 GMT-0500 (CDT) }

What's the right way to store/retrieve a date in mongo-js?

Upvotes: 2

Views: 472

Answers (1)

kushyar
kushyar

Reputation: 1191

DateTime and Date values should be converted to UTC Time before storing them in MongoDB

The method you have above works fine in nodejs.

Here is how you retrieve it:

new Date(parseInt(this.toString().slice(0,8), 16)*1000);

More info

Upvotes: 1

Related Questions