user2344293
user2344293

Reputation: 173

How to get Today and Past 7 Days Records in Meteor JS?

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?

Upvotes: 2

Views: 1383

Answers (2)

chridam
chridam

Reputation: 103475

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

Upvotes: 4

Dineshaws
Dineshaws

Reputation: 2083

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

Upvotes: 0

Related Questions