0

I need some help, because I'm struggling for some time with this issue, I'm trying to filter the days from this week, excluding today and yesterday, because I have separate stacks for them. I want to filter only days that are older than yesterday, but between this week. Other filter is that I want days older than this week (from last 2 weeks).

So here is my code:

    const filterByDate = (data) => {
        const tempStacks = {} as any;
        const date = new Date();
        const yesterday = new Date(new Date().setDate(date.getDate() - 1));
        console.log('yesterday', yesterday.getDate())
        const getWeek = (d) => {
            const dt: any = new Date(d.getFullYear(),0,1);
            return Math.ceil((((d - dt) / 86400000) + dt.getDay())/7);
        };
        const thisWeek = getWeek(date);
        console.log('this week', thisWeek)
        console.log('test date----', new Date(new Date().setDate(new Date().getDate() - 7)))
        tempStacks.today = data.filter(obj => {
            return obj.date.getDate() === date.getDate() 
            && obj.date.getMonth() === date.getMonth() 
            && obj.date.getFullYear() === date.getFullYear()
        });
        tempStacks.yesterday = data.filter(obj => {
            return obj.date.getDate() === yesterday.getDate() 
            && obj.date.getMonth() === yesterday.getMonth() 
            && obj.date.getFullYear() === yesterday.getFullYear()
        });
        console.log('tempStacks yesterday', tempStacks.yesterday)
        tempStacks.thisWeek = data.filter(obj => {
            return  getWeek(obj.date) === thisWeek
        })
        tempStacks.lastTwoWeeks = data.filter(obj => {
            return getWeek(obj.date) === thisWeek-1 || getWeek(obj.date) === thisWeek-2 
        })
        return tempStacks;
    }

The problem that I have with my code is that in thisWeek stack, I have all days from this week, including yesterday and today and I want them to be excluded, since I have separate stacks for them.

Anyone knows a fine solution for this issue that I have?

Christian
  • 53
  • 1
  • 8
  • Does this answer your question? [How to subtract days from a plain Date?](https://stackoverflow.com/questions/1296358/how-to-subtract-days-from-a-plain-date) – pilchard Nov 11 '22 at 23:33
  • 1
    simply create a date that 2 days ago and one that is 7 days ago (see above duplicate) and then filter by `filter(obj => obj.date > [now-7] && obj.date < [now -2]);` – pilchard Nov 11 '22 at 23:36
  • That's way too much code for a simple filter. You don't need all those comparisons. Use the suggestions by @pilchard and you can condense your code down to a few lines. – Yogi Nov 12 '22 at 00:29
  • The requirement is entirely unclear, e.g. what constitutes a week (Mon to Sun, Sun to Sat, Sat to Fri, and so on), what if "yesterday" was in the previous week, etc. Please provide sample input and expected output that covers the obvious and less obvious cases. – RobG Nov 12 '22 at 10:39
  • `const yesterday = new Date(new Date().setDate(date.getDate() - 1))` modifies *date* such that after it's executed, `yesterday.getTime() == date.getTime()`. – RobG Nov 12 '22 at 10:53
  • 1
    @pilchard Thank you very much for your answer, I've tried your solution and it works fine for me. Many thanks and many beers! Cheers – Christian Nov 12 '22 at 20:46
  • @RobG I think that the specs are refering to last 7 days for this week and last 14 days for last two weeks.. I hope :) But thanks for your solution also, If this will be the case I will apply it also. Cheers and beers! – Christian Nov 12 '22 at 20:48

1 Answers1

0

As @pilchard suggests, create a date for 2 days ago, then for the previous Monday to get the first set of dates.

Then get the Monday prior to that to get the second set of dates. e.g.

// Return Date for previous Monday or
// same date if date is Monday.
// Returned date is at 00:00:00.000
function priorMonday (date = new Date()) {
  return new Date(date.getFullYear(), date.getMonth(),
          date.getDate() - (date.getDay() || 7) + 1);
}

// Get ranges for filters
let d = new Date();
let data = []; // Put whatever in here

// week2end is yesterday at 00:00:00.000
let week2end = new Date(d.getFullYear(), d.getMonth(), d.getDate() - 1);

// week2start is previous Monday at 00:00:00.000
let week2start = priorMonday(week2end);

// week1start is previous Monday at 00:00:00.000
let week1start = priorMonday(new Date(week2start - 1));

// Show ranges
console.log(`Wk 1 start: ${week1start.toDateString()}` +
  `\nWk 2 start: ${week2start.toDateString()}` +
  `\nWk 2 end  : ${week2end.toDateString()}`);  

// Week 1 dates are greater than or equal to week 1 start,
// and less than week 2 start
let week1 = data.filter(o => o.date >= week1start && o.date < week2start);

// Week 2 dates are greater than or equal to week 2 start
// and less than yesterday (so up to and including 2 days ago)
let week2 = data.filter(o => o.date >= week2start && o.date < week2end);
RobG
  • 142,382
  • 31
  • 172
  • 209