简体   繁体   中英

Moment js getting next date given specified week day

I seem to have a bit of a problem getting the previous Monday given a particular date. I'm trying to use Moment js for the task. Obviously, I can do it by hand, but found it curious that I couldn't get it to work using the example in the moment.js documentation on their website: http://momentjs.com/docs/#/get-set/day/ .

I was trying something like:

moment([2013, 08, 15, 15, 20]).day(-1).format('ddd, MMM DD')

which results in the 'two days ago' date, that being September 13 instead of the expected September 9th.

Does anybody have a clue here? Thanks.

Here is how it works:

moment().day(1) // this monday
moment().day(-6) // last monday, think of it as this monday - 7 days = 1 - 7 = -6

Same applies in other direction:

moment().day(8) // next monday, or this monday + 7 days = 1 + 7 = 8

Your code moment().day(-1) can be explained as this Sunday - 1 day = 0 - 1 = -1 or this Saturday - 7 days = 6 - 7 = -1

The accepted answer only works if you already know whether the day in question is in this week or next week. What if you don't know? You simply need the next available Thursday following some arbitrary date?

First, you want to know if the day in question is smaller or bigger than the day you want. If it's bigger, you want to use the next week. If it's smaller, you can use the same week's Monday or Thursday.

const dayINeed = 4; // for Thursday
if (moment().isoWeekday() <= dayINeed) { 
  return moment().isoWeekday(dayINeed);
} else...

If we're past the day we want already (if for instance, our Moment is a Friday, and we want the next available Thursday), then you want a solution that will give you "the Thursday of the week following our moment", regardless of what day our moment is, without any imperative adding/subtracting. In a nutshell, you want to first go into the next week, using moment().add(1, 'weeks') . Once you're in the following week, you can select any day of that week you want, using moment().day(1) .

Together, this will give you the next available day that meets your requirements, regardless of where your initial moment sits in its week:

const dayINeed = 4; // for Thursday

// if we haven't yet passed the day of the week that I need:
if (moment().isoWeekday() <= 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 day
  return moment().add(1, 'weeks').isoWeekday(dayINeed);
}

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

I think the point is that using day() or isoWeekday() you get a date in the current week, no matter which day of the week is today. As a consequence, the date you get can be past, or still to come.

Example:

if today is Wednesday, moment().isoWeekday(5).format() would return the date of the upcoming Friday.

While moment().isoWeekday(1).format() would return the previous Monday.

So when you say you want the date of, let's say, "last Tuesday", this date could belong to the current week or to the previous week, depending on which day is today.

A possible function to get the date of the last dayOfTheWeek is

function getDateOfPreviousDay(dayOfTheWeek) {
  currentDayOfTheWeek = moment().isoWeekday();   
  if ( currentDayOfTheWeek >= dayOfTheWeek ) {
    return moment().isoWeekday(dayOfTheWeek).format(); // a date in the current week
  }
  else {
    return moment().add(-1,'weeks').isoWeekday(dayOfTheWeek).format(); // a date in the previous week
  }
}
function nextWeekday (day, weekday) {
  const current = day.day()
  const days = (7 + weekday - current) % 7
  return day.clone().add(days, 'd')
}

// example: get next Friday starting from 7 Oct 2019

nextWeekday(moment('2019-10-07'), 5)) // 2019-10-11

 const upcomingDay = (dayIndex, format = "DD MMMM YYYY") => { if ( Number(moment().format("D")) >= Number(moment().day(dayIndex).format("D")) ) { return moment().day(7 + dayIndex).format(format); } return moment().day(dayIndex).format(format); };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM