0

I would like to check, if a rendered day in a webcalendar-element is X-mas Eve or the first of January or another date of a year and if so, colour that date differently.

So if the day rendered is the third Monday in May, colour it differently. If it is X-mas eve, colour it differently and so forth.

All ive found so far is how to extract the day to a specific date. But I would like to do kinda the opposite. Has anyone done that and can offer some tips?

3
  • 1
    What input do you have, and what part specifically are you having trouble with? Is it generating a list of holidays, looping over a list of dates, comparing dates, applying a CSS class to specific date cells or anything else? Commented Nov 20, 2013 at 12:40
  • What have you tried? If you're looking for "the first Monday in May" this should not be hard to find (tip: work out what day May 1st is and do the math...). Commented Nov 20, 2013 at 12:40
  • The problem was that I didnt really now to start. I knew that I had to compare dates and calculate but couldnt really find a good starting point. But Tim and Jon went in the right direction. Commented Nov 20, 2013 at 13:29

2 Answers 2

6

It's not really clear what you mean by "do kinda the opposite" but:

static IsThirdMondayInMay(DateTime date)
{
    // The first X in a month is always in the range [1, 8)
    // The second X in a month is always in the range [8, 15)
    // The third X in a month is always in the range [15, 22)
    return date.Month == 5 && date.DayOfWeek == DayOfWeek.Monday &&
           date.Day >= 15 && date.Day < 22;
}

static IsChristmasEve(DateTime date)
{
    return date.Month == 12 && date.Day == 24;
}

Or more generally for the last:

static MonthDayMatches(DateTime date, int month, int day)
{
    return date.Month == month && date.Day == day;
}

then:

bool christmasEve = MonthDayMatches(date, 12, 24);
Sign up to request clarification or add additional context in comments.

1 Comment

I think you pretty much got it. While I've only found how to find out whether 2008/09/21 is a Monday, I havent found how to do the opposite: Determine, whether the Tuesday that is being rendered is the 3rd Tuesday of May of the current year or something. But again, I think you pretty much nailed it. I'm gonna work form there and see how far I get with it. Thanx a million!
2

I assume you are using the ASP.NET Calendar control. Then use the DayRender event. This has an argument Day with a property Date which is the DateTime. Now you can use this date to decide whether it is a special day or not.

void DayRender(Object source, DayRenderEventArgs e)
{
    DateTime date = e.Day.Date;  // here it is
    if(IsSpecialDay(date))  // your method to determine if a given date is a "special"-date
        e.Cell.BackColor = System.Drawing.Color.Gold;  // or use the Style property to use CSS
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.