c# - Check if a DayOfWeek is within two specified DayOfWeek's -


i have dayofweek , need check see if day between 2 other dayofweek variables.

for example:

dayofweek monday = dayofweek.monday; dayofweek friday= dayofweek.friday;  dayofweek today = datetime.today.dayofweek;  if (today between monday , friday) {    ... } 

note: these days inclusive. in case, if day monday, tuesday, wednesday, thursday, , friday, it's valid.

the thing can think of doing massive if statement in different method, perhaps extension method, isn't elegant.

edit

here example of requirements:

    public enum recurringmodes     {         minutes,         hours     }      public recurringmodes recurringmode { get; set; }     public int recurringvalue { get; set; }      ...       public ienumerable<datetime> allduedatestoday()     {         //get current date (starting @ 00:00)         datetime current = datetime.today;          //get today , tomorrow's day of week.         dayofweek today = current.dayofweek;         dayofweek tomorrow = current.adddays(1).dayofweek;          //if isn't in date range, return nothing today.         if (!isindaterange(today, startingon, endingon))             yield break;          while (current.dayofweek != tomorrow)         {             //check selected recurring mode             switch (recurringmode)             {                 //if it's minutes, add desired minutes                 case recurringmodes.minutes:                     current = current.addminutes(recurringvalue);                     break;                 //if it's hours, add desired hours.                 case recurringmodes.hours:                     current = current.addhours(recurringvalue);                     break;             }              //add calculated date collection.             yield return current;         }     }      public bool isindaterange(dayofweek day, dayofweek start, dayofweek end)     {         //if same date         if (start == end && start == day)             return true;          //this if statement problem lies.         if ((start <= end && (day >= start && day <= end)) ||             (start > end && (day <= start && day >= end)))             return true;         else return false;     } 

effectively, method allduedatestoday() return list of datetime represents schedule today.

you can compare enums if numbers:

if (today >= monday && today <= friday) { 

as @tyrsius points out, works because monday < friday. so, technically, need check first:

if ((monday <= friday && (today >= monday && today <= friday)) ||     (monday > friday  && (today <= monday && today >= friday))) { 

note .nets week starts @ sunday: dayofweek.sunday 0.

if want week start @ monday, have perform arithmetic.

var lowlimit = ((int)monday + 6) % 7; var highlimit = ((int)friday + 6) % 7; var valuetocheck = ((int)today + 6) % 7;  if ((lowlimit <= highlimit && (valuetocheck >= lowlimit && valuetocheck <= highlimit)) ||     (lowlimit > highlimit  && (valuetocheck <= lowlimit && valuetocheck >= highlimit))) { 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -