简体   繁体   English

如何使用moment js获得与2个月重叠的一周中的星期一?

[英]How to get monday of the week which overlaps 2 months using moment js?

I need to create few records on Mondays using an api.我需要在星期一使用 api 创建一些记录。

And the rule to get the Monday is that it should be either first Monday of a given month or last Monday of the given month.获取星期一的规则是它应该是给定月份的第一个星期一或给定月份的最后一个星期一。

For example:例如:

If the input date range is 1 Sep 2020 - 30 Sep 2020, I need to pull 28 Sep 2020.如果输入日期范围是 2020 年 9 月 1 日 - 2020 年 9 月 30 日,我需要拉 2020 年 9 月 28 日。

If the input date range is 1 Oct 2020 - 31 Oct 2020, I need to pull 26 Oct 2020.如果输入日期范围是 2020 年 10 月 1 日 - 2020 年 10 月 31 日,我需要拉 2020 年 10 月 26 日。

If the input date range is 1 Nov 2020 - 30 Nov 2020, I need to pull 30 Nov 2020.如果输入日期范围是 2020 年 11 月 1 日 - 2020 年 11 月 30 日,我需要提取 2020 年 11 月 30 日。

If the input date range is 1 Nov 2020 - 31 Dec 2020, I need to pull 28 Dec 2020.如果输入日期范围是 2020 年 11 月 1 日 - 2020 年 12 月 31 日,我需要拉 2020 年 12 月 28 日。

What I did so far is I pulled all the Mondays of given month and stored them in an array.到目前为止,我所做的是提取给定月份的所有星期一并将它们存储在一个数组中。 I tried pulling 1st or last Monday of the month, but this works for few months and not for few others.我尝试拉取本月的第一个或最后一个星期一,但这适用于几个月而不适用于其他几个。

Hence, I was thinking if I get to know the week of a given day(Monday), which overlaps 2 months, then I can pull that Monday.因此,我在想,如果我了解与 2 个月重叠的给定日期(星期一)的那一周,那么我可以拉那个星期一。

So my question is how to get Monday of the week which overlaps 2 months?所以我的问题是如何获得与 2 个月重叠的每周星期一? I am using moment js.我正在使用时刻js。

Maybe too late, but it could be helpful for future viewers.也许为时已晚,但它可能对未来的观众有所帮助。

The function takes start and end , but we only use end , so you can get rid of start as all the operations will be on end date in order to get the last monday.该函数采用startend ,但我们只使用end ,因此您可以摆脱start因为所有操作都将在end日期以获得最后一个星期一。 To achieve our goal we need to do two simple steps:为了实现我们的目标,我们需要做两个简单的步骤:

  1. Get the end of the month with the end date.获取带有end日期的end
  2. Get the monday of the previous "calculated" date.获取上一个“计算”日期的星期一。

For the first step, we use End of Time in order to get the end of the month (the last day).第一步,我们使用End of Time来获取月末(最后一天)。

Mutates the original moment by setting it to the end of a unit of time.通过将原始时刻设置为单位时间的结尾来改变原始时刻。

Then we can get the Monday of the week the date is, and we can use Date of Week with the date of previous step.然后我们可以得到日期所在的星期几,我们可以使用星期几和上一步的日期。

Gets or sets the day of the week.获取或设置星期几。 [...] As of 2.1.0, a day name is also supported. [...] 从 2.1.0 开始,还支持日期名称。 This is parsed in the moment's current locale.这是在当前的当前语言环境中解析的。

And taking your examples, we can test our function:并以您的示例为例,我们可以测试我们的功能:

 function lastMonday(start, end) { console.log('Start is ', moment(start).format('LL')); console.log('End is ', moment(end).format('LL')); return moment(end).endOf('month').day('Monday'); } console.log('Last monday is', lastMonday('2020-09-01', '2020-09-30').format('LL')); console.log('Last monday is', lastMonday('2020-10-01', '2020-10-31').format('LL')); console.log('Last monday is', lastMonday('2020-11-01', '2020-11-30').format('LL')); console.log('Last monday is', lastMonday('2020-11-01', '2020-12-31').format('LL'));
 <script src="https://momentjs.com/downloads/moment.js"></script>

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

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