Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime DayOfWeek Comparison

Tags:

c#

.net

datetime

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

like image 568
Greycrow Avatar asked Jan 30 '10 22:01

Greycrow


2 Answers

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();
like image 96
Andrew Hare Avatar answered Sep 29 '22 10:09

Andrew Hare


From MSDN:

The value of the constants in the DayOfWeek enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).

So you can also use greater than and less than operators for your calculation.

//Check if Saturday YET
if (MYWorkDay.DayOfWeek < DayOfWeek.Saturday && MYWorkDay.DayOfWeek > DayOfWeek.Sunday)
    IGottaWork();
else
    Party();
like image 44
pure_eval Avatar answered Sep 29 '22 11:09

pure_eval