28

在给定特定日期的情况下,我似乎在获取前一个星期一时遇到了一些问题。我正在尝试使用 Moment js 来完成这项任务。显然,我可以手动完成,但奇怪的是我无法使用他们网站上的 moment.js 文档中的示例来让它工作:http: //momentjs.com/docs/#/get-set /天/

我正在尝试类似的东西:

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

这导致“两天前”日期,即 9 月 13 日而不是预期的 9 月 9 日。

有人在这里有线索吗?谢谢。

4

4 回答 4

62

下面是它的工作原理:

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

同样适用于其他方向:

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

您的代码moment().day(-1)可以解释为这个星期日 - 1 天 = 0 - 1 = -1 或这个星期六 - 7 天 = 6 - 7 = -1

于 2013-09-30T17:52:28.050 回答
16

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

于 2016-09-21T11:20:02.603 回答
2
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
于 2019-10-13T12:22:22.893 回答
1

我认为关键是无论今天是一周中的哪一天,都可以使用day()或获得当前一周的日期。isoWeekday()因此,您得到的日期可能已经过去,也可能还在未来。

例子:

如果今天是星期三,moment().isoWeekday(5).format()将返回即将到来的星期五的日期。

虽然 moment().isoWeekday(1).format()将返回前一个星期一。

所以当你说你想要“上周二”的日期时,这个日期可能属于本周或前一周,具体取决于今天是哪一天。

获取最后日期的可能函数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
  }
}
于 2017-01-10T13:27:00.857 回答