javascript - filter array of objects on click by 'last month' and 'last week' using moment.js -
i have datecreated 2015-12-11t11:12:14.635z coming database every object in array
i want filter array last week , last month
problem if today 19th march, search 11th 18th last 7 days , seems search last 7 days calculating 24 hours * 7 searches need start 00:00:01 each day.
now 1st want calculate last week first.. based on current date using moment.js , convert above format can filter data
basically, want calculate last week based on current timestamp.
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 javascript filter use lodash , 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) ; });
Comments
Post a Comment