How to determine if selected dated from Ajax Calendar control is a weekend or not?

Im using the ajax calendar extender. When I select a date from the calendar it outputs into a text box. Now I want to find out of the selected date was a weekend day or weekday date. How do I manage to execute this? Help is appreciated.

weekendLit.Text = (selectedDate.DayOfWeek.ToString() == "Saturday" || selectedDate.DayOfWeek.ToString() == "Sunday") ? "Weekend" : "Weekday";

^^^What if he isn’t using english, I think you want to skip the .ToString() materialization there.

He’s probably also not using a literal named weekendLit. :stuck_out_tongue:

Just a jumping off point.

Ah thank you!

English will do.

Right, but you still have 2 ToString() calls and a comparatively more expensive string equality check there. You can just compare the DateTime.DayOfWeek without invoking string comparison.

I see. How’s this?

weekendLit.Text = (thisDate.DayOfWeek == DayOfWeek.Saturday || thisDate.DayOfWeek == DayOfWeek.Sunday) ? "Weekend" : "Weekday";

I guess I’m a bit rusty these days. I’ve been learning Ruby.

That looks pretty correct to me.

I shudder at figuring out equality in languages where one can monkeypatch . . .

I’m seriously regretting my decision to step into Ruby on Rails. I’m probably going to back out now while I’m still sane. Ahhh, my types feel stronger already.

Never second guess learning something new. Even if you don’t end up using it, at least you’ll understand it. I just went through this process with Event Sourcing over at the DDD group on yahoo. I probably won’t use it all, but at least I know how if I’m ever asked to.

@Serenarules, of course you’re correct. It’s just such a huge step backwards compared to ASP.NET MVC. I just got annoyed over the constant change in .NET but Rails is a far greater offender on this.

I’m weary of google sending me to 404s and unmaintained/outdated articles. google is not your friend in this case. I’m on my own learning this. I guess I’m ok with that.

In stark contrast, the .NET community will literally fall over themselves to help. Just saying I appreciate you and wwb_99, et al.

I think this smells better:

public bool IsWeekend(DateTime date)
{
	return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday;
}

For what it’s worth, I use something like this:

public static class Day
{
    public static bool IsSunday(DateTime date)
    {
        return date.DayOfWeek == DayOfWeek.Sunday;
    }
    public static bool IsMonday(DateTime date)
    {
        return date.DayOfWeek == DayOfWeek.Monday;
    }
    public static bool IsTuesday(DateTime date)
    {
        return date.DayOfWeek == DayOfWeek.Tuesday;
    }
    public static bool IsWednesday(DateTime date)
    {
        return date.DayOfWeek == DayOfWeek.Wednesday;
    }
    public static bool IsThursday(DateTime date)
    {
        return date.DayOfWeek == DayOfWeek.Thursday;
    }
    public static bool IsFriday(DateTime date)
    {
        return date.DayOfWeek == DayOfWeek.Friday;
    }
    public static bool IsSaturday(DateTime date)
    {
        return date.DayOfWeek == DayOfWeek.Saturday;
    }
    public static bool IsWeekDay(DateTime date)
    {
        return IsMonday(date)
            || IsTuesday(date)
            || IsWednesday(date)
            || IsThursday(date)
            || IsFriday(date);
    }
    public static bool IsWeekEnd(DateTime date)
    {
        return IsSunday(date)
            || IsSaturday(date);
    }
}

And is used like:


bool isWeekEnd = Day.IsWeekEnd(date);

Static helper classes are your friends. Could also be written as DateTime extension methods.

Nice. Much more complete.