C# How can I check if today is the first Monday of the month?

前端 未结 5 1094
太阳男子
太阳男子 2021-01-01 12:04

How can I check if today is the first Monday of the month?

The code below gives me the last day of the month, how should I modify this?

DateTime toda         


        
5条回答
  •  清酒与你
    2021-01-01 12:44

    A related case is, as Jon said, slightly trickier. Presuming you know the year and month:

    public static DateTime getFirstMondayOfMonthAndYear(int Month, int Year)
    {
        DateTime dt;
        DateTime.TryParse(String.Format("{0}/{1}/1", Year, Month), out dt); 
        for (int i = 0; i < 7; i++)
            {
            if (dt.DayOfWeek == DayOfWeek.Monday)
            {
                return dt;
            }
            else
            {
                dt = dt.AddDays(1);
            }
            }
        // If get to here, punt
        return DateTime.Now;
    }
    

提交回复
热议问题