Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an ISO date object in javascript

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

db.schedule_collection.find({
  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

like image 819
Joel James Avatar asked Mar 06 '13 20:03

Joel James


People also ask

What is ISO date JavaScript?

Definition and Usage. The toISOString() method returns a date object as a string, using the ISO standard. The standard is called ISO-8601 and the format is: YYYY-MM-DDTHH:mm:ss.sssZ.

How do I convert a date to an ISO?

The date. toISOString() method is used to convert the given date object's contents into a string in ISO format (ISO 8601) i.e, in the form of (YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss.

Is ISO date UTC?

ISO 8601. The format seen in your first example 2019-11-14T00:55:31.820Z is defined by the ISO 8601 standard. The T in the middle separates the year-month-day portion from the hour-minute-second portion. The Z on the end means UTC, that is, an offset-from-UTC of zero hours-minutes-seconds.


5 Answers

Try using the ISO string

var isodate = new Date().toISOString()

See also: method definition at MDN.

like image 61
Mathletics Avatar answered Oct 04 '22 12:10

Mathletics


try below:

var temp_datetime_obj = new Date();

collection.find({
    start_date:{
        $gte: new Date(temp_datetime_obj.toISOString())
    }
}).toArray(function(err, items) { 
    /* you can console.log here */ 
});
like image 25
Abdullah Shahin Avatar answered Oct 04 '22 12:10

Abdullah Shahin


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);

like image 22
iheartweb Avatar answered Oct 04 '22 13:10

iheartweb


This worked for me:

var start = new Date("2020-10-15T00:00:00.000+0000");
 //or
start = new date("2020-10-15T00:00:00.000Z");

collection.find({
    start_date:{
        $gte: start
    }
})...etc
new Date(2020,9,15,0,0,0,0) may lead to wrong date: i mean non ISO format (remember javascript count months from 0 to 11 so it's 9 for october)
like image 34
Adel Ben Hamadi Avatar answered Oct 04 '22 12:10

Adel Ben Hamadi


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);

$.ajax({
    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);

        db.collection('schedule_collection').find(
            { start_date: { $gte: start_date } }
        ).toArray( function (err,d){
            ...
            res.json(d)
        })
    }
};

Note: I'm using Express and Mongoskin.

like image 39
fgborja Avatar answered Oct 04 '22 14:10

fgborja