使用 javascript 检测每个月的最后一周
javascript 中有什么方法可以检测每个(当前)月份的最后一周。还是这个月的最后一个星期一?
what would be a way in javascript to detect the last week of each (current) month. Or last monday of the month?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我建议获取该月的天数,然后从最后一天开始循环,直到 getDay() 返回星期一 (1) 或星期日 (0) .. 根据您一周的开始时间。一旦你得到开始日期......结束日期将是 startDate + 7 所以
我发现了这些内容 这有帮助:
然后循环:
I would suggest to get the number of days in the month and then loop from the last day until getDay() gives back a Monday (1) or Sunday(0) .. based on when does your week start. Once you get your start date ... end date would be startDate + 7 so something along these lines
I found this helpful :
Then the loop:
使用 date 对象及其方法,您可以执行以下操作..
更新
到该月最后一个星期一的完整计算可以压缩为
详细的示例。
Playing with the date object and its methods you can do the following..
update
the complete calculations to get to last monday of the month could be compacted to
verbose example..
使用 getDay() 获取该月最后一天的星期几并从中进行计算(从该月的天数中减去该值应该可以解决问题。+/- 1) 。
Use
getDay()
to get the day of week of the last day in month and work from that (substracting the value from the number of days of the month should probably do the trick. +/- 1).要确定是否是星期一,请使用
.getDay() == 1
。要确定是否是该月的最后一天,请添加 7 天并比较月份:nextMonday.setDate(monday.getDate()+7);
nextMonday.getMonth() == monday.getMonth();
To determine whether it is a Monday, use
.getDay() == 1
. To determine if it is the last of the month, add seven days and compare months:nextMonday.setDate(monday.getDate()+7);
nextMonday.getMonth() == monday.getMonth();
Javascript“Date”对象是你的朋友。
这将为您提供所选日期(0 到 7)所在月份最后一天的日期(月份中的日期,如 30)。
查找该月的最后周将取决于您的意思。如果您指的是最后完整周,那么(如果您指的是星期日 - 星期六)找到最后一个星期六,然后减去 6。如果您指的是开始的最后一周月,找到最后一个星期日。
The Javascript "Date" object is your friend.
That'll give you the date (in the month, like 30) of the last day of the month that's the chosen day of the week (0 through 7).
Finding the last week of the month will depend on what you mean by that. If you mean the last complete week, then (if you mean Sunday - Saturday) find the last Saturday, and subtract 6. If you mean the last week that starts in the month, find the last Sunday.
您可能还想查找给定日期之前或之后的第三个星期一或第一个星期二,
或标记两个日期之间的每周三。
You may also like to find the third Monday or the first Tuesday before or after a given date,
or flag every Wednesday between two dates.
我发现这样的示例可以检测每周的最后一个星期一,但它不会检测该月的最后一个星期一。也许这将有助于找到更好的解决方案,该代码看起来很短。
i found such example that detects last monday of each week but it wont detect last monday of the month. maybe it will help to find better solution, that code looks short.
好的,到目前为止,我想出了这样的解决方案,使它有点像我自己的方式,并得到了这里提到的一些东西。它工作正常并且始终返回当月的最后一个星期一。
OK, so far i came up with such solution making it a bit of my own way and getting a few things mentioned here. It works correct and always returns the last monday of current month.
获取该月的最后一天:
获取最后一个星期一:
获取给定日期的一年中的某一周:
将它们放在一起(假设您正在使用 Firebug):
Get the last day of the month:
Get the last Monday:
Get week of the year for a given day:
Putting them together (assuming you're using Firebug):