create an iso date object in javascript

create an iso date object in javascript  using -'javascript,node.js,mongodb,coffeescript'

I have a mongo database set up.
creating a new date object in mongoDb create a date object in ISO format
eg: ISODate("2012-07-14T00:00:00Z")

I am using node.js to connect to mongo database and query the database.
when ever I create a new date object (new Date()) in javascript its creates a javascript date object eg: Wed Mar 06 2013 14:49:51 GMT-0600 (CST)

Is there a way to create an ISO date object in javascript so that I can send the object directly to the mongoDb and perform date query  

I am able to perform the below query in mongoDb

  start_date: { '$gte': new Date(2012, 01, 03, 8, 30) }

but cannot perform when I send in an javascript date object from node

The mongodb cookbook provides an python example to query the mongo database using datetime module, but does not provide any example use javascript.

Any help is appreciated.
Thanking you in advance

asked Sep 21, 2015 by sandeep bhadauria
0 votes
1 view

3 Answers

0 votes

I solved this problem instantiating a new Date object in node.js:...

In Javascript, send the Date().toISOString() to nodejs:...

var start_date = new Date(2012, 01, 03, 8, 30);

    type: 'POST',
    data: { start_date: start_date.toISOString() },
    url: '/queryScheduleCollection',
    dataType: 'JSON'
}).done(function( response ) { ... });

Then use the ISOString to create a new Date object in nodejs:..

exports.queryScheduleCollection = function(db){
    return function(req, res){

        var start_date = new Date(req.body.start_date);

            { start_date: { $gte: start_date } }
        ).toArray( function (err,d){

Note: I'm using Express and Mongoskin.

answered Sep 21, 2015 by bhavin
0 votes

In node, the Mongo driver will give you an ISO string, not the object. (ex: Mon Nov 24 2014 01:30:34 GMT-0800 (PST)) So, simply convert it to a js Date by: new Date(ISOString);

answered Sep 21, 2015 by rajeevr642
0 votes

try below:

var temp_datetime_obj = new Date();

        $gte: new Date(temp_datetime_obj.toISOString())
}).toArray(function(err, items) { 
    /* you can console.log here */ 
answered Sep 21, 2015 by balvant maurya