简体   繁体   English

Moment js在给定指定工作日的情况下获取下一个日期

[英]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.我正在尝试使用 Moment js 来完成任务。 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/ .显然,我可以手动完成,但奇怪的是我无法使用他们网站上 moment.js 文档中的示例让它工作: http://momentjs.com/docs/#/get-set /天/

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.这导致“两天前”日期,即 9 月 13 日而不是预期的 9 月 9 日。

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你的代码moment().day(-1)可以解释为这个星期天 - 1 天 = 0 - 1 = -1 或这个星期六 - 7 天 = 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') .简而言之,您想先进入下一周,使用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) .进入下一周后,您可以使用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另见: 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.我认为关键是使用day()isoWeekday()可以获得当前周的日期,无论今天是一周中的哪一天。 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.如果今天是星期三, moment().isoWeekday(5).format()将返回即将到来的星期五的日期。

While moment().isoWeekday(1).format() would return the previous Monday.moment().isoWeekday(1).format()将返回前一个星期一。

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获取最后dayOfTheWeek的日期的可能函数是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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