Find next instance of a given weekday (ie. Monday)

2019-01-18 09:44发布

I want to get the date of the next Monday or Thursday (or today if it is Mon or Thurs). As Moment.js works within the bounds of a Sunday - Saturday, I'm having to work out the current day and calculate the next Monday or Thursday based on that:

if (moment().format("dddd")=="Sunday") { var nextDay = moment().day(1); }
if (moment().format("dddd")=="Monday") { var nextDay = moment().day(1); }
if (moment().format("dddd")=="Tuesday") { var nextDay = moment().day(4); }
if (moment().format("dddd")=="Wednesday") { var nextDay = moment().day(4); }
if (moment().format("dddd")=="Thursday") { var nextDay = moment().day(4); }
if (moment().format("dddd")=="Friday") { var nextDay = moment(.day(8); }
if (moment().format("dddd")=="Saturday") { var nextDay = moment().day(8); }

This works, but surely there's a better way!

8条回答
Deceive 欺骗
2楼-- · 2019-01-18 09:58

The following can be used to get any next weekday date from now (or any date)

var weekDayToFind = moment().day('Monday').weekday(); //change to searched day name

var searchDate = moment(); //now or change to any date
while (searchDate.weekday() !== weekDayToFind){ 
  searchDate.add(1, 'day'); 
}
查看更多
Emotional °昔
3楼-- · 2019-01-18 09:59

Here's a solution to find the next Monday, or today if it is Monday:

const dayOfWeek = moment().day('monday').hour(0).minute(0).second(0);

const endOfToday = moment().hour(23).minute(59).second(59);

if(dayOfWeek.isBefore(endOfToday)) {
  dayOfWeek.add(1, 'weeks');
}
查看更多
Emotional °昔
4楼-- · 2019-01-18 10:02

Here's e.g. next Monday:

var chosenWeekday = 1 // Monday

var nextChosenWeekday = chosenWeekday < moment().weekday() ? moment().weekday(chosenWeekday + 7) : moment().weekday(chosenWeekday)
查看更多
Anthone
5楼-- · 2019-01-18 10:03

The trick here isn't in using Moment to go to a particular day from today. It's generalizing it, so you can use it with any day, regardless of where you are in the week.

First you need to know where you are in the week: moment.day(), or the slightly more predictable (in spite of locale) moment().isoWeekday().

Use that to know if today's day is smaller or bigger than the day you want. If it's smaller/equal, you can simply use this week's instance of Monday or Thursday...

const dayINeed = 4; // for Thursday
const today = moment().isoWeekday();

if (today <= dayINeed) { 
  return moment().isoWeekday(dayINeed);
}

But, if today is bigger than the day we want, you want to use the same day of next week: "the monday of next week", regardless of where you are in the current week. In a nutshell, you want to first go into next week, using moment().add(1, 'weeks'). Once you're in next week, you can select the day you want, using moment().day(1).

Together:

const dayINeed = 4; // for Thursday
const today = moment().isoWeekday();

// if we haven't yet passed the day of the week that I need:
if (today <= dayINeed) { 
  // then just give me this week's instance of that day
  return moment().isoWeekday(dayINeed);
} else {
  // otherwise, give me *next week's* instance of that same day
  return moment().add(1, 'weeks').isoWeekday(dayINeed);
}

See also https://stackoverflow.com/a/27305748/800457

查看更多
爷的心禁止访问
6楼-- · 2019-01-18 10:06

moment().day() will give you a number referring to the day_of_week.

What's even better: moment().day(1 + 7) and moment().day(4 + 7) will give you next Monday, next Thursday respectively.

See more: http://momentjs.com/docs/#/get-set/day/

查看更多
在下西门庆
7楼-- · 2019-01-18 10:09

IMHO more elegant way:

var setDays = [ 1, 1, 4, 4, 4, 8, 8 ],
    nextDay = moment().day( setDays[moment().day()] );
查看更多
登录 后发表回答