javascript 给定指定工作日的时刻 js 获取下一个日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19100373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moment js getting next date given specified week day
提问by Tudor Vintilescu
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/.
我似乎在获取给定特定日期的前一个星期一时遇到了一些问题。我正在尝试使用 Moment js 来完成任务。显然,我可以手动完成,但发现我无法使用他们网站上的 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.
有人在这里有线索吗?谢谢。
回答by mechanicalfish
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
回答by XML
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)
.
如果我们已经过了我们想要的那一天(例如,如果我们的时刻是星期五,而我们想要下一个可用的星期四),那么您需要一个解决方案,该解决方案将为您提供“我们时刻之后的一周中的星期四”,不管我们的时刻是哪一天,没有任何必要的加法/减法。简而言之,您想先进入下周,使用moment().add(1, 'weeks')
. 进入下一周后,您可以使用 选择该周中的任何一天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);
}
回答by zeroquaranta
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.
我认为关键是使用day()
orisoWeekday()
你在本周获得一个日期,无论今天是一周中的哪一天。因此,您得到的日期可能已经过去,也可能即将到来。
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
}
}
回答by Simone Sanfratello
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