Create an ISO date object in javascript

前端 未结 5 1491
遇见更好的自我
遇见更好的自我 2020-12-05 04:02

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 usi

相关标签:
5条回答
  • 2020-12-05 04:33

    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 */ 
    });
    
    0 讨论(0)
  • 2020-12-05 04:34

    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.

    0 讨论(0)
  • 2020-12-05 04:35

    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)

    0 讨论(0)
  • 2020-12-05 04:49

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

    0 讨论(0)
  • 2020-12-05 04:50

    Try using the ISO string

    var isodate = new Date().toISOString()
    

    See also: method definition at MDN.

    0 讨论(0)
提交回复
热议问题