Reputation: 966
I have a dateCreated as 2015-12-11T11:12:14.635Z coming from database for every object in an array
I want to filter that array for the last week and last month
Problem is if today is the 19th March, i was to search from the 11th to the 18th for the last 7 days and that seems to search for the last 7 days by calculating 24 hours * 7 by my searches need to start from 00:00:01 each day.
now i 1st want to calculate last week first.. based on current date using moment.js and then i will convert it to the above format so that i can filter data
Basically, I want to calculate last week based on current timestamp.
Upvotes: 1
Views: 12909
Reputation: 3362
Here is my way of filtering by
- Today
- Last 7 Days (Last Week, inclusive)
- Last 30 days (Last Month, inclusive)
const today = moment();
const lastSevenDays = moment().subtract(7, 'days');
const lastThirtyDays = moment().subtract(30, 'days');
const FILTER_MAP = {
All: () => true,
Today: task => task.timestamp.isBetween(today, today, 'day', '[]'), // [] means inclusive
Week: task => task.timestamp.isBetween(lastSevenDays, today, 'day', '[]'),
Month: task => task.timestamp.isBetween(lastThirtyDays, today, 'day', '[]'),
};
const DATA = [
{ id: "0", name: "Eat", timestamp: moment().subtract(6, 'days') },
{ id: "1", name: "Sleep", timestamp: moment() },
{ id: "2", name: "Pawn", timestamp: moment().subtract(28, 'days') },
{ id: "3", name: "Repeat", timestamp: moment().add(1, 'days') } //testing purpose + 1 day
];
Upvotes: 0
Reputation: 730
this should get you to the point in time 7 days ago at 00:00:00
var time7daysAgo = moment().subtract(7,'days').startOf('day');
var time30daysAgo = moment().subtract(30,'days').startOf('day');
after that you just format to whatever you like. Say to a milisecond unix timestamp:
var time7daysAgoMiliseconds = time7daysAgo.format('x');
comparison on numbers is quite handy after this point.
Upvotes: 1
Reputation: 966
I did like this.. and its working for me..
_this.lastWeek = function () { var result = moment().subtract(7,'days').hours(0); return result._d; }; _this.lastMonth = function () { var result = moment().subtract(30,'days').hours(0); return result._d; };
then i filtered my array using underscoreJS.. (YOU CAN USE loadash also)..
_this.thisWeekData = _.filter(_this.inbox, function(inbox) { return (moment(inbox.createdAt) > _this.lastWeek()); }); _this.lastWeekData = _.filter(_this.inbox, function(inbox) { return ((moment(inbox.createdAt) < _this.lastWeek()) && (moment(inbox.createdAt) > _this.lastMonth())); }); _this.lastMonthData = _.filter(_this.inbox, function(inbox) { return (moment(inbox.createdAt) < _this.lastMonth()); });
if you dont want to do all this stuff in your controller then there is angular library for variety of filters.. you can directly use it in html.. "Angular-filter"
angular-filter library link is here
Thankyou guys for all your support..
Upvotes: 3
Reputation: 1530
the dates should be:
var last7DayStart = moment().startOf('day').subtract(1,'week');
var lastMonthThisDay = moment().startOf('day').subtract(1,'month');
var yesterdayEndOfRange = moment().endOf('day').subtract(1,'day');
then if it is a javascript filter i would use lodash and do:
var javascriptArrayOfObjectsWithDates = [
{ date : '2015-12-11T11:12:14.635Z', anotherProperty: 0 },
{ date : moment().subtract(1, 'day' ).format(), testThis: 'works!'}
];
var filteredObjects = _.filter(javascriptArrayOfObjectsWithDates,
function(each){
return moment(each.date)
.isBetween(last7DayStart, yesterdayEndOfRange) ;
});
Upvotes: 6