3

Possible Duplicate:
How to find the 3rd Friday in a month with C#?

There's a condition that a specific event will occur only on first and third Wednesday of every month. I am trying to achieve this like below,

DateTime nextMeeting = DateTime.Today.AddDays(14);
int daysUntilWed = ((int)DayOfWeek.Wednesday - (int)nextMeeting.DayOfWeek + 7) % 7;
DateTime nextWednesday = nextMeeting.AddDays(daysUntilWed);

But I am not getting the desired result. I am sure, I am missing out on a logic or is there any method in ASP.NET through which I can do that? More Detail: I am trying to display the next Wednesday (whichever is first) by clicking on a button which will set the label.

Community
  • 1
  • 1
unknownsatan
  • 731
  • 5
  • 16
  • 24

1 Answers1

1

Try this:

var FirstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

//first wednesday
while (FirstDat.DayOfWeek != DayOfWeek.Wednesday)
       FirstDay = FirstDate.AddDays(1);

//3rd wednesday
var ThirdWed = FirstDay.AddDays(14);
rs.
  • 26,707
  • 12
  • 68
  • 90