node.js 在javascript中创建一个ISO日期对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15257911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:46:49  来源:igfitidea点击:

Create an ISO date object in javascript

javascriptnode.jsmongodbcoffeescript

提问by Joel James

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

我设置了一个 mongo 数据库。在 mongoDb 中创建一个新的日期对象以 ISO 格式创建一个日期对象,例如: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)

我正在使用 node.js 连接到 mongo 数据库并查询数据库。当我new Date()在 javascript 中创建一个新的日期对象()时,它会创建一个 javascript 日期对象,例如: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

有没有办法在javascript中创建ISO日期对象,以便我可以将对象直接发送到mongoDb并执行日期查询

I am able to perform the below query in mongoDb

我能够在 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

但是当我从节点发送一个 javascript 日期对象时无法执行

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

mongodb cookbook 提供了一个使用 datetime 模块查询 mongo 数据库的 python 示例,但没有提供任何使用 javascript 的示例。

Any help is appreciated. Thanking you in advance

任何帮助表示赞赏。提前致谢

回答by Mathletics

Try using the ISO string

尝试使用ISO 字符串

var isodate = new Date().toISOString()

See also: method definition at MDN.

另请参阅:MDN 上的方法定义

回答by Abdullah Shahin

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 */ 
});

回答by iheartweb

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

在 node 中,Mongo 驱动程序会给你一个 ISO 字符串,而不是对象。(例如:)Mon Nov 24 2014 01:30:34 GMT-0800 (PST)因此,只需通过以下方式将其转换为 js 日期:new Date(ISOString);

回答by fgborja

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

我在 node.js 中实例化一个新的 Date 对象解决了这个问题:...

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

在 Javascript 中,将 Date().toISOString() 发送到 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:..

然后使用 ISOString 在 nodejs 中创建一个新的 Date 对象:..

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.

注意:我使用的是 Express 和 Mongoskin。