tags:

views:

233

answers:

4

Id like to Compare a date to see if it is before Saturday like so:

        //Check if Saturday YET
        if (MYWorkDay.DayOfWeek < DateTime.DayOfWeek.Saturday)
            IGottaWork();
        else
            Party();

There seems to be no way to do this.

Is there a way?

Thanks in advance

+3  A: 

Why not this?

    if (MYWorkDay.DayOfWeek != DayOfWeek.Saturday
          && MYWorkDay.DayOfWeek != DayOfWeek.Sunday) 
    {
        IGottaWork();
    }
    else
        Party();

Or even better:

List<DayOfWeek> partyDays = new List<DayOfWeek> {
    DayOfWeek.Saturday, DayOfWeek.Sunday
};

if (partyDays.Contains(MYWorkDay.DayOfWeek))
    Party();
else
    IGottaWork();
Andrew Hare
I see what I was doing wrong. I don't need DateTime before the DayOfWeek. Silly me :)Thanks for the help
Greycrow
I just posted on my blog (didn't seem appropriate as an answer here) two of my favorite custom extension methods In and Between. Sometimes I think I spend too much time trying to make my code look like a document. http://www.josheinstein.com/blog/index.php/2010/01/in-between
Josh Einstein
Josh Einstein
@Josh - That is because I edited my answer to remove them just before you edited my logic error. A perfect storm of edits! :)
Andrew Hare
A: 

Try this:

if (new [] {DayOfWeek.Saturday, DayOfWeek.Sunday}.Contains(d.DayOfWeek)) {
  // party :D
} else {
  // work  D:
}
John Feminella
+1  A: 

DayOfWeek is an enum starting with Sunday as 0 and Saturday as the last element hence 6 in integer terms. Think of that when comparing.

Maxwell Troy Milton King
A: 

If you would rather do comparisons rather than checking a list, you could also do this:

if ((MYWorkDay.DayOfWeek.CompareTo(DayOfWeek.Sunday) > 0) && (MYWorkDay.DayOfWeek.CompareTo(DayOfWeek.Saturday) < 0))
{
      IGottaWork();
}
else
{
      Party();
}
wsanville