2

How to get records today and last 7 days records in Meteor JS.I am using created date is

var date = new Date();

I am using MongoDB collection code as shown below:

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

What to do?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2344293
  • 173
  • 1
  • 13
  • http://stackoverflow.com/questions/563406/add-days-to-datetime just remove 7 days from new Date(), compare and it should be fine – sdooo Apr 16 '15 at 09:26

2 Answers2

4

Try to subtract the number of days from the current date's timestamp:

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

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

Using momentjs API is much intuitive and easier to understand:

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

Note: To get the native Date object that Moment.js wraps, use toDate()

chridam
  • 100,957
  • 23
  • 236
  • 235
  • Not getting,I am using created field in collection in this collection field assign new date().in DB Collection stores like this "created" :ISODate("2015-04-16T08:54:06.543Z"). @chridam – user2344293 Apr 16 '15 at 08:59
  • Ur posted answer not working weekAgoDate cames NaN.Can you check once.@chridam – user2344293 Apr 16 '15 at 11:40
  • @user2344293 I've updated the answer, can you try and let me know how that change goes? – chridam Apr 16 '15 at 12:17
  • 1
    Worked great using momentjs, I just had to add .toDate() to 'weekAgoDate' and 'today' when using them in the query. Actually, I even had to use moment() again instead of today to instantiate the variable 'weekAgoDate', as shown below. var today = moment().toDate(); var weekAgoDate = moment().subtract(7, "days").toDate(); var fetchResult = Profile.find({created:{$gte: weekAgoDate, $lt: today}}); – Jean-Paul Manuel Apr 05 '16 at 12:23
0

Please look over following snippet

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

Function frame_date definition here-

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;

}

thanks

Dineshaws
  • 2,065
  • 16
  • 26