如何获得今天和过去7天记录流星JS?

如何获得记录今天和流星JS.I过去的7天记录我使用创建日期如何获得今天和过去7天记录流星JS?

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

怎么办?

回答:

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

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

回答:

请查看下面的代码片段

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;

}

感谢

以上是 如何获得今天和过去7天记录流星JS? 的全部内容, 来源链接: utcz.com/qa/258141.html

回到顶部