C#:找出哪个星期一是该月的第三个星期一?
如果我正在编写一些运行一年的日期(按天迭代)的 C# 代码,并且希望每月的第三个星期一发生一些特别的事情,我该如何实现这一目标?
换句话说,查找当前星期一是每月的哪个星期一的最佳方法是什么?
If I'm writing some C# code that runs through a year of dates (iterating by day) and want something special to happen every 3rd Monday of the month, how can I accomplish this?
In other words, what is the best way to find which Monday of the month a current Monday is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你的“换句话说”并没有真正重申你首先描述的问题,所以我会回答这两个问题。
这是一个相当简单的方法,它将确定给定年份的给定月份中一周中特定日期的第 n 次出现。
确定某个日期是该月的哪个星期一(或任何其他日期)要容易得多;只需取该月中某天的上限/7:
I don't think your "in other words" really restates the problem that you describe first, so I'll answer both.
Here's a fairly simple method that will determine the nth occurrence of a particular day of the week in a given month in a given year.
Determining which Monday (or any other day) of the month a date is is much easier; just take the ceiling of the day of the month / 7:
查找 15 日到 21 日之间的星期一(含 15 日和 21 日)。
Find the Monday that is between the 15th and the 21st, inclusive.
我不知道是否有一个日期操作库可以完成您想要的操作,但是您可以非常轻松地编写自己的函数:
I don't know if there is a date manipulation library to do what you want, but you can write your own functions pretty easily: