Home Ask Login Register

Developers Planet

Your answer is one click away!

Aviv Paz February 2016

angular - filter all the objects that the startDate and EndDate is in range of month and year that was selected

I want to filter and get all the campaigns that are in range of the month and year that is being sent. For example, StartDate - 2/1/16 EndDate - 1/2/17 year - 2016 month - 4 I want this campaign to show on all the months from Feb' 2016 to Jan' 2017

app.filter('dateFilter', function () {
    return function (campaigns, year, month) {
        var filtered = [];
        if (!campaigns)
            return null;
        for (var i = 0; i < campaigns.length; i++) {
            var startDate = new Date(campaigns[i].startTime);
            var endDate = new Date(campaigns[i].endTime);

            if ( Validation )
                filtered.push(campaigns[i]);
        }
        return filtered;
    };
});

Answers


Aviv Paz February 2016

Ok found the answer, it was pretty easy after all. I just created the startDate and endDate with an empty day value and checked the range.

 for (var i = 0; i < campaigns.length; i++) {
            var startDate = new Date(campaigns[i].startTime);
            var endDate = new Date(campaigns[i].endTime);
            startDate = new Date(startDate.getFullYear(), startDate.getMonth());
            endDate = new Date(endDate.getFullYear(), endDate.getMonth());
            var dateCheck = new Date(year, month - 1);
            if (dateCheck >= startDate && dateCheck <= endDate)
                filtered.push(campaigns[i]);
        }

Post Status

Asked in February 2016
Viewed 1,315 times
Voted 14
Answered 1 times

Search




Leave an answer


Quote of the day: live life