ral1239 — 2011-12-04T22:58:44-05:00 — #1
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.
imaginekitty — 2011-12-05T08:01:38-05:00 — #2
weekendLit.Text = (selectedDate.DayOfWeek.ToString() == "Saturday" || selectedDate.DayOfWeek.ToString() == "Sunday") ? "Weekend" : "Weekday";
wwb_99 — 2011-12-05T09:43:42-05:00 — #3
^^^What if he isn't using english, I think you want to skip the .ToString() materialization there.
imaginekitty — 2011-12-05T09:50:36-05:00 — #4
He's probably also not using a literal named weekendLit.
Just a jumping off point.
ral1239 — 2011-12-05T11:50:38-05:00 — #5
Ah thank you!
English will do.
wwb_99 — 2011-12-05T16:40:16-05:00 — #6
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.
imaginekitty — 2011-12-05T16:55:38-05:00 — #7
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.
wwb_99 — 2011-12-05T21:25:29-05:00 — #8
That looks pretty correct to me.
I shudder at figuring out equality in languages where one can monkeypatch . . .
imaginekitty — 2011-12-09T00:18:46-05:00 — #9
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.
serenarules — 2011-12-09T02:40:39-05:00 — #10
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.
imaginekitty — 2011-12-09T07:31:18-05:00 — #11
@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.
imaginekitty — 2011-12-09T23:41:30-05:00 — #12
I think this smells better:
public bool IsWeekend(DateTime date)
return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday;
serenarules — 2011-12-10T03:03:39-05:00 — #13
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)
public static bool IsWeekEnd(DateTime date)
And is used like:
bool isWeekEnd = Day.IsWeekEnd(date);
Static helper classes are your friends. Could also be written as DateTime extension methods.
imaginekitty — 2011-12-10T08:13:57-05:00 — #14
Nice. Much more complete.