Create an ISO date object in javascript

前端 未结 5 1602
遇见更好的自我
遇见更好的自我 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: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.

提交回复
热议问题