C#:找出哪个星期一是该月的第三个星期一?

发布于 2024-09-01 18:54:01 字数 110 浏览 14 评论 0原文

如果我正在编写一些运行一年的日期(按天迭代)的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

缱倦旧时光 2024-09-08 18:54:01
public bool IsThirdMondayOfMonth(DateTime dt)
{
  if(dt.DayOfWeek == DayOfWeek.Monday && dt.Day > 14 && dt.Day <= 21)
  {
    return true;
  }
  return false;
}
public bool IsThirdMondayOfMonth(DateTime dt)
{
  if(dt.DayOfWeek == DayOfWeek.Monday && dt.Day > 14 && dt.Day <= 21)
  {
    return true;
  }
  return false;
}
檐上三寸雪 2024-09-08 18:54:01

我认为你的“换句话说”并没有真正重申你首先描述的问题,所以我会回答这两个问题。

这是一个相当简单的方法,它将确定给定年份的给定月份中一周中特定日期的第 n 次出现。

public static DateTime DayOccurrence(int year, int month, DayOfWeek day, 
    int occurrenceNumber)
{
    DateTime start = new DateTime(year, month, 1);
    DateTime first = start.AddDays((7 - ((int)start.DayOfWeek - (int)day)) % 7);

    return first.AddDays(7 * (occurrenceNumber - 1));
}

确定某个日期是该月的哪个星期一(或任何其他日期)要容易得多;只需取该月中某天的上限/7:

public static int DayOccurrence(DateTime date)
{
    return (int)Math.Ceiling(date.Day / 7.0);
}

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.

public static DateTime DayOccurrence(int year, int month, DayOfWeek day, 
    int occurrenceNumber)
{
    DateTime start = new DateTime(year, month, 1);
    DateTime first = start.AddDays((7 - ((int)start.DayOfWeek - (int)day)) % 7);

    return first.AddDays(7 * (occurrenceNumber - 1));
}

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:

public static int DayOccurrence(DateTime date)
{
    return (int)Math.Ceiling(date.Day / 7.0);
}
ι不睡觉的鱼゛ 2024-09-08 18:54:01

查找 15 日到 21 日之间的星期一(含 15 日和 21 日)。

Find the Monday that is between the 15th and the 21st, inclusive.

洋洋洒洒 2024-09-08 18:54:01

我不知道是否有一个日期操作库可以完成您想要的操作,但是您可以非常轻松地编写自己的函数:

using System;

class Program {
    static void Main(string[] args) {
        int year = 2010;
        int month = 05;
        DateTime thirdMonday = ThirdMonday(year, month);
    }

    private static DateTime ThirdMonday(int year, int month) {
        for (int day = 1; day <= DateTime.DaysInMonth(year, month); ++day) {
            DateTime dt = new DateTime(year, month, day);
            if (dt.DayOfWeek == DayOfWeek.Monday) {
                return dt.AddDays(14);
            }
        }
        // this should never happen
        throw new Exception();
    }
}

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:

using System;

class Program {
    static void Main(string[] args) {
        int year = 2010;
        int month = 05;
        DateTime thirdMonday = ThirdMonday(year, month);
    }

    private static DateTime ThirdMonday(int year, int month) {
        for (int day = 1; day <= DateTime.DaysInMonth(year, month); ++day) {
            DateTime dt = new DateTime(year, month, day);
            if (dt.DayOfWeek == DayOfWeek.Monday) {
                return dt.AddDays(14);
            }
        }
        // this should never happen
        throw new Exception();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文