0

What is a quick way to determine if a certain date:

  • is next week
  • is last week
  • is this week
  • etc

I am open to using Date, Moment, Date-fns, or Dayjs if any of those have a built-in method.

As of now, I am thinking of using getDay() and comparing if the date is not too far ahead and not too near to today.

Thanks,

John Mc
  • 331
  • 1
  • 3
  • 12
  • 1
    month is trivial, week, use whichever of those libraries includes iso week calculations – Jaromanda X Sep 17 '22 at 01:59
  • to get the week, an idea will be to [find the first and last day of the week for a given date](https://stackoverflow.com/a/5210450/17447) and check if the [date difference in days](https://stackoverflow.com/a/15289883/17447) is less than than 7. For the same week thing, do a greater than-less than comparison – naveen Sep 17 '22 at 02:03

2 Answers2

0

This example is assuming you're defining "next week" as "7-14 days in the future", but you could tweak this answer to do whatever you want. This answer is intended to help understand how to perform this sort of operation on javascript Dates, not just this operation specifically.

Option 1: Determine how many days are between the target date and the current date by subtracting the current date from the target date

// store current date as "today"
var today = new Date();

// store 8 days from today as "later"
later = new Date();
later.setDate(later.getDate() + 8);

// Determine how many days are between the two dates by subtracting "today" from "later" and converting milliseconds to days
days = (later - today) / (1000 * 3600 * 24);

// Is "later" 7-14 days away?
console.log(days > 7 && days < 14); // true

Option 2: Store the start and end date and compare those to our target date

// store 8 days from today as "later"
later = new Date();
later.setDate(later.getDate() + 8);

// Store the start date
start = new Date(); 
start.setDate(start.getDate() + 7); 

// Store the end date
end = new Date(); 
end.setDate(end.getDate() + 14); 

// is "later" between the start and end date?
console.log(later > start && later < end); // true

Gradyn Wursten
  • 118
  • 2
  • 18
  • Well "next week" is not 7 days into the future. But I think the comment to my original question by @Jaromando X solved it, I need the ISO week number, and work from there. – John Mc Sep 17 '22 at 03:37
0

One algorithm is to take both dates back to the start of the week, then get the number of weeks between the two, e.g.

// Get start of week (previous or current Monday)
function getWeekStart(d = new Date()) {
  return new Date(d.getFullYear(), d.getMonth(), d.getDate() - (d.getDay() || 7) + 1);
}

// Get relative weeks difference
function getRelativeWeek(d = new Date()) {
  let thisWeek = getWeekStart();
  let targetWeek = getWeekStart(d);
  let diffWeeks = Math.round((targetWeek - thisWeek) / (8.64e7*7));
  // Convert to words
  switch (true) {
    case (diffWeeks < -1):
      return Math.abs(diffWeeks) + ' weeks ago';
    case (diffWeeks == -1):
      return 'last week';
    case (diffWeeks == 0):
      return 'this week';
    case  (diffWeeks == 1):
      return 'next week';
    case (diffWeeks > 1):
      return diffWeeks + ' weeks away';
    default : 
      return 'dunno';
  }
}

// Examples
let d = new Date();
// Current week
[new Date(+d - 25*8.64e7),
 new Date(+d - 7*8.64e7),
 d,
 new Date(+d + 7*8.64e7),
 new Date(+d + 25*8.64e7),
 new Date(NaN)
].forEach(d => console.log(
  d.toDateString() + ': ' + getRelativeWeek(d)
));

Note that 8.64e7*7 is approximately the number of milliseconds in a week. It may be ±3.6e6 depending on whether the week has a daylight saving boundary or not, which will cause a non–integer result after division. Rounding fixes that.

RobG
  • 142,382
  • 31
  • 172
  • 209