【问题标题】:How to get Today and Past 7 Days Records in Meteor JS?如何在 Meteor JS 中获取今天和过去 7 天的记录?
【发布时间】:2015-04-16 08:27:20
【问题描述】:

如何在 Meteor JS 中获取今天和过去 7 天的记录。我使用的创建日期是

var date = new Date();

我使用的MongoDB集合代码如下图:

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

var start = new Date(""+yyyy+"-"+mm+"-"+dd+"T00:00:00.000Z");
var end = new Date(""+yyyy+"-"+mm+"-"+(dd+1)+"T00:00:00.000Z");

var fetchResult = Profile.find({created:{$gte: start, $lt: end}});

怎么办?

【问题讨论】:

标签: mongodb meteor mongodb-query


【解决方案1】:

尝试从当前日期的时间戳中减去天数:

var today = new Date();
var weekAgoDate = new Date();
weekAgoDate.setUTCDate(weekAgoDate.getDate() - 7);

var fetchResult = Profile.find({created:{$gte: weekAgoDate, $lt: today}});

使用 momentjs API 更加直观易懂:

var today = moment();
var weekAgoDate = today.subtract("days", 7); // same as today.add("days", -7)

var fetchResult = Profile.find({created:{$gte: weekAgoDate.toDate(), $lt: today.toDate()}});

注意:要获取 Moment.js 包装的原生 Date 对象,请使用 toDate()

【讨论】:

  • 没有得到,我在这个集合字段中使用集合中的创建字段分配新的日期()。在数据库集合存储中,像这样“创建”:ISODate(“2015-04-16T08:54:06.543 Z”)。 @chridam
  • 你发布的答案不工作 weekAgoDate 来了 NaN。你能检查一次吗。@chridam
  • @user2344293 我已经更新了答案,你能试着告诉我这个变化是怎么回事吗?
  • 使用momentjs 效果很好,我只需要在查询中使用它们时将.toDate() 添加到'weekAgoDate' 和'today'。实际上,我什至不得不再次使用 moment() 而不是 today 来实例化变量 'weekAgoDate',如下所示。 var 今天 = moment().toDate(); var weekAgoDate = moment().subtract(7, "days").toDate(); var fetchResult = Profile.find({created:{$gte: weekAgoDate, $lt: today}});
【解决方案2】:

请看下面的sn-p

var today_date = new Date(frame_date());
var range_date = new Date(today_date);

range_date.setDate(today_date.getDate() - 1);    // for toady records
Profile.find({ 'created': {$gte: range_date, $lte: today_date}})  


range_date.setDate(today_date.getDate() - 7);   // for last 7 days  records
Profile.find({ 'created': {$gte: range_date, $lte: today_date}})  

函数frame_date在这里定义-

function frame_date() {
    var time = require('time');
    var timestamp = new time.Date();
//        var timestamp = new Date();
    timestamp.setTimezone("Australia/Sydney");  //you can set timezone here
    var getYear = timestamp.getFullYear();
    var getMnth = timestamp.getMonth();
    var getDate = timestamp.getDate();
    var gethours = timestamp.getHours();
    var getMins = timestamp.getMinutes();
    var getSecs = timestamp.getSeconds();
    var getMilisecs = timestamp.getMilliseconds();
    if (getDate < 10) {
        getDate = "0" + getDate;
    }
    if (getMnth < 9) {
        getMnth = parseInt(getMnth) + 1
        getMnth = "0" + getMnth;
    } else {
        getMnth = getMnth + 1;
    }
    if (gethours < 10) {
        gethours = "0" + gethours;
    }
    if (getMins < 10) {
        getMins = "0" + getMins;
    }
    if (getSecs < 10) {
        getSecs = "0" + getSecs;
    }
    var getMiliSecs = getMilisecs;
    if (getMilisecs < 10) {
        getMiliSecs = "0" + getMilisecs;
    } else if (getMilisecs < 100) {
        getMiliSecs = "00" + getMilisecs;
    } else {
        getMiliSecs = getMilisecs;
    }


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

}

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 2021-01-09
    • 2011-01-03
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多