programing

javascript에서 ISO 날짜 개체를 만듭니다.

lastcode 2023. 3. 8. 21:13
반응형

javascript에서 ISO 날짜 개체를 만듭니다.

mongo 데이터베이스를 설정했습니다. mongoDb에서 새 날짜 개체를 만들고 ISO 형식의 날짜 개체를 만듭니다. 예:ISODate("2012-07-14T00:00:00Z")

node.js를 사용하여 mongo 데이터베이스에 접속하여 데이터베이스를 조회합니다.새로운 날짜 개체를 만들 때마다(new Date()javascript에서는 다음과 같은 javascript 날짜 개체를 만듭니다.Wed Mar 06 2013 14:49:51 GMT-0600 (CST)

오브젝트를 mongoDb로 직접 전송하여 날짜 쿼리를 실행할 수 있도록 javascript에서 ISO 날짜 오브젝트를 만드는 방법이 있습니까?

mongoDb에서 아래 쿼리를 실행할 수 있습니다.

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

노드에서 javascript 날짜 개체를 보낼 때 수행할 수 없습니다.

mongodb cookbook은 datetime 모듈을 사용하여 mongo 데이터베이스를 쿼리하는 python 예를 제공하지만 javascript를 사용하는 예는 제공하지 않습니다.

어떤 도움이라도 감사합니다.잘 부탁드립니다.

ISO 문자열 사용 시도

var isodate = new Date().toISOString()

MDN에서의 메서드의 정의도 참조해 주세요.

아래를 시험해 보세요.

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

노드에서는 Mongo 드라이버가 오브젝트가 아닌 ISO 문자열을 제공합니다.(예:Mon Nov 24 2014 01:30:34 GMT-0800 (PST)따라서 js 날짜로 변환하기만 하면 됩니다.new Date(ISOString);

이 방법은 효과가 있었습니다.

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)

node.js:...에서 새로운 날짜 개체를 인스턴스화하는 문제를 해결했습니다.

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 ) { ... });

그런 다음 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)
        })
    }
};

메모: Express와 Mongoskin을 사용하고 있습니다.

언급URL : https://stackoverflow.com/questions/15257911/create-an-iso-date-object-in-javascript

반응형