0

Possible Duplicate:
Getting specific days in a month

I've touched on this problem once before, in How to find the 3rd Friday in a month with C#? But since I did not explain my problem well then, I must try again:

My goal here is simple: upon the press of a button (referred to here as "Button1"), I must determine whether today's date is prior to either the first, or the third Wednesday of the month. If this is the case, I must then set the text of a label (referred to here as "lblDate") to the date of whichever of these future Wednesdays is nearest to the current date.

So far, I've written this:

protected void Button1_Click(object sender, EventArgs e)
{

    DateTime Now = DateTime.Today;
    DateTime TempDate = new DateTime(Now.Year, Now.Month, 1);

    if (TempDate.DayOfWeek != DayOfWeek.Wednesday)
    {
        TempDate = TempDate.AddDays(1);
        string date = TempDate.ToString();
        lblDate.Text = date;
    }
    if (TempDate == TempDate.AddDays(1))
    {
        TempDate = TempDate.AddDays(14);
        string date = TempDate.ToString();
        lblDate.Text = date;
    }

}

As you can see, something is missing. I would greatly appreciate any assistance in filling that in...

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

2 Answers2

2

Well I'm not entirely sure this will answer your question but it might get you on the right track. You can figure out the day of the week from the DateTime feature. Just use something similar to:

DateTime dateValue = new DateTime(2008, 6, 11);
Console.WriteLine((int) dateValue.DayOfWeek);      // Displays 3

Use that in conjunction with the actual date say something like:

if((int) dateValue.DayOfWeek == 3) //which is Wednesday
  if(date < 7 && date > 1) 
     week == 1st Weds of month
  else(date < 21 && date > 14)
     week == 3rd Weds of month

That's not exact code obviously but perhaps something along those lines would help out a little. And you will have to adjust the parameters a bit in order to adjust for the 1st not falling exactly on a monday. Since there is only seven days in a week and even if the first falls on a tuesday it just fall within the range of 1 and 7, likewise for 14 and 21. But just play around with that and you should figure the answer out soon enough.

John Verber
  • 745
  • 2
  • 16
  • 31
2

You could also try it like this:

protected DateTime getFirstWednesdayOfMonth(DateTime seedDate)
{
    DateTime wed1 = new DateTime(seedDate.Year, seedDate.Month, 1); //1st Wednesday can start on the 1st of the month
    while (wed1.DayOfWeek != DayOfWeek.Wednesday)
    {
        wed1 = wed1.AddDays(1);
    }
    return wed1;
}

protected DateTime getThirdWednesdayOfMonth(DateTime seedDate)
{
    DateTime wed3 = new DateTime(seedDate.Year, seedDate.Month, 15); //3rd Wednesday cannot start prior to the 15th of the month
    while (wed3.DayOfWeek != DayOfWeek.Wednesday)
    {
        wed3 = wed3.AddDays(1);
    }
    return wed3;
}
protected void Button1_Click(object sender, EventArgs e)
{
    DateTime Now = DateTime.Today;
    DateTime wed1 = getFirstWednesdayOfMonth(Now);
    DateTime wed3 = getThirdWednesdayOfMonth(Now);

    if (Now < wed1)
    {
        lblDate.Text = wed1.ToString();
    }
    else if (Now < wed3)
    {
        lblDate.Text = wed3.ToString();
    }
}
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
pete
  • 24,141
  • 4
  • 37
  • 51