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

前端 未结 5 1095
太阳男子
太阳男子 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:42

    How about:

    DateTime today = DateTime.Today;
    if (today.DayOfWeek == DayOfWeek.Monday && today.Day <= 7)
    

    Finding the first Monday of the month is slightly trickier. It's not clear whether or not you need that - let us know if you need that.

    Note the use of DateTime.Today once - that way you don't end up with potential oddities where the date changes between the two parts of the condition being evaluated.

提交回复
热议问题