使用Javascript自动将日期调整为每月的第二个星期六?
我需要网站的 Javascript 代码来自动调整日期。目标是让代码自动将以下语句调整为从现在到永远每个月的第二个星期六:
Next membership meeting: Saturday, MONTH, DAY, YEAR 11 a.m. to noon.
有人有主意吗?非常感谢!
I need Javascript code for a website to automatically adjust a date. The goal is to have the code automatically adjust the following statement to be the second Saturday of every month from now until eternity:
Next membership meeting: Saturday, MONTH, DAY, YEAR 11 a.m. to noon.
Anyone have an idea? Much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该函数将为您获取日期对象,您可以从中提取您需要的内容:
This function will get you the date object, you can pull out what you need from it:
我没有测试,但这是基础知识:
I didn't test but here is the basics:
所以,我认为你的问题的核心是:我如何知道每个月的第二个星期六是什么?
未经测试,但这就是我想出的:
它是针对任何一个月的任何第 n 天进行抽象的。
第 2 周在该月的 14 天范围内。
我们可以:
首先减去本月开始的星期几的偏移量,
然后第二:
我们可以减去我们要查找的星期几的偏移量。
(这需要是天数的偏移量,因此星期六是 0(零)偏移量。我们从第 n 天的绝对值减去一周中的天数得到这个值。
这给了我们 如果我们在第二个星期六
然后,因为您有一些整数,所以您可以对这些值进行简单的比较,
之前,则显示该日期,如果没有计算下个月的新日期,
希望有所帮助。
So, I figure that the meat of your problem is: How do I know what the second saturday of each month is?
Not tested, but this is what I came up with:
It is abstracted for any nth day of any month.
The 2nd week is in the range of 14 days into the month.
We can:
first subtract the offset for the day of the week that this month starts with,
then second:
we can subtract the offset for the day of the week that we are looking for.
(this needs to be the offset of days, so saturday is a 0 (zero) offset. We get this value from the absolute value of nth day minus the number of days in the week.
This gives us the date of the second saturday.
Then, because you have some ints you can do a simple compare against the values.
If we're before the second saturday, display that, if not calculate a new date for next month.
Hope that helps.