Home > Software engineering >  Javascript calculate days between last week and yesterday
Javascript calculate days between last week and yesterday

Time:11-13

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?

CodePudding user response:

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

  • Related