2015-03-13 25 views

回答

0

在mysql中,我使日期时间字段为yearmonthdayhrminsec .. 20150313132300这种方式完全数字化,不依赖于不同的数据库处理日期和时间的方式。这样我可以查询数字范围.....对我来说无论如何要容易得多。

0

我在这里给出一些代码行。请根据您的要求修改以下代码。

以下函数frame_date()将根据小时,分钟,秒和毫秒获得时间。

function frame_date(gethours, getMins, getSecs, getMiliSecs) { 

    var timestamp = new Date(); 
    var getYear = timestamp.getFullYear(); 
    var getMnth = timestamp.getMonth(); 
    var getDate = timestamp.getDate(); 
    if (getDate < 10) { 
     getDate = "0" + getDate; 
    } 
    if (getMnth < 9) { 
     getMnth = parseInt(getMnth) + 1 
     getMnth = "0" + getMnth; 
    } else { 
     getMnth = getMnth + 1; 
    } 


    var final_framed_date = getYear + "-" + getMnth + "-" + getDate + " " + gethours + ":" + getMins + ":" + getSecs + "." + getMiliSecs; 
    return final_framed_date; 

} 

现在我们将在新的Date()函数中调用frame_date()函数来获取日期。

var nine_am_date = new Date(frame_date(09, 00, 00, 000));// get time of 9 a.m. 

var seven_pm_date = new Date(frame_date(19, 00, 00, 000));// get time of 7 p.m. 

现在我们将使用大于或小于创建的字段。它只会在9点之间取回。和晚上7点

db.collection('users').find({created: {$gte: nine_am_date, $lte: seven_pm_date}}).sort({created: -1}).toArray(function(err, users) { 
    if (err) { 
     //handle error here 
    } else { 
     //do stuff here 
    } 
}); 

感谢

相关问题