Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do I find the first or second weekday of month using C# DateTeime method.
Posted

Fairly simple. Find the day of week of the 1st of the month, check if it is a weekday, it not, add one day to the date and continue doing it until it is a weekday.
 
Share this answer
 
Try this:
C#
public static DateTime? FindMonthWeekDay( int year, int month, int offset )
{
  if ( offset < 1 || offset > DateTime.DaysInMonth( year, month ) )
  {
    throw new ArgumentOutOfRangeException( "offset" );
  }

  DateTime moment = new DateTime( year, month, 1 );
  while ( moment.Month == month )
  {
    DayOfWeek dayOfWeek = moment.DayOfWeek;
    if ( dayOfWeek != DayOfWeek.Saturday && dayOfWeek != DayOfWeek.Sunday )
    {
      offset--;
    }
    if ( offset == 0 )
    {
      return moment;
    }
    moment = moment.AddDays( 1 );
  }
  return null;
} // FindMonthWeekDay

Use offset=1 for the first day, offset=2 for the second day...
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900