SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Code Csharp:
    weekendLit.Text = (selectedDate.DayOfWeek.ToString() == "Saturday" || selectedDate.DayOfWeek.ToString() == "Sunday") ? "Weekend" : "Weekday";

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,625
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    ^^^What if he isn't using english, I think you want to skip the .ToString() materialization there.

  4. #4
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    He's probably also not using a literal named weekendLit.

    Just a jumping off point.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah thank you!

    English will do.

  6. #6
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,625
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I see. How's this?
    Code Csharp:
    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.

  8. #8
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,625
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    That looks pretty correct to me.

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

  9. #9
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    ... 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.

  10. #10
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    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.

  11. #11
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @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.

  12. #12
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I think this smells better:
    Code csharp:
    public bool IsWeekend(DateTime date)
    {
    	return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday;
    }

  13. #13
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    For what it's worth, I use something like this:

    Code Csharp:
    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:

    Code Csharp:
    bool isWeekEnd = Day.IsWeekEnd(date);

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

  14. #14
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Nice. Much more complete.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •