Home > Software design >  How to determine if a Date is next week in Javascript?
How to determine if a Date is next week in Javascript?

Time:09-17

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,

CodePudding user response:

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

CodePudding user response:

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.

  • Related