c# - Generate list of dates of the following work week -


i'm trying generate list of dates based on following condition:

  • if today wednesday , after 6:00pm or after wednesday, generate next work week.

  • else generate current work week.

work week being defined monday-friday.

so far googling , pulling 2 answers here, have extension gives me next occurring specified day based on current day:

public static datetime next(this datetime dt, dayofweek startoofweek) {     var start = (int)dt.dayofweek;     var target = (int)startoofweek;     if (target <= start)     {         target += 7;     }      return dt.adddays(target - start); } 

pulled from: how date of next sunday?

and have following work days as:

var daysofweek = enum.getvalues(typeof(dayofweek))             .oftype<dayofweek>()             .skip(1)             .takewhile(day => day != dayofweek.saturday)             .tolist(); 

i proceed test such:

foreach (var day in daysofweek) {     debug.writeline(datetime.now.next(day).date.tostring(cultureinfo.currentculture)); } 

since today sunday 4/24/16, correct output next week:

> 4/25/2016 0:00:00 > 4/26/2016 0:00:00 > 4/27/2016 0:00:00 > 4/28/2016 0:00:00 > 4/29/2016 0:00:00 

but if today wednesday 4/27/16, get:

> 5/2/2016 0:00:00 > 5/3/2016 0:00:00 > 5/4/2016 0:00:00 > 4/28/2016 0:00:00 > 4/29/2016 0:00:00 

i'm getting closer, i'm wondering if there easier way i've done or if there's answer out there already.

this might seem overkill, i'd break small functions. first, date, given day of week, previous instance of weekday, including current date? (translation - given date, date of previous monday, including date if monday?)

then, given date , number of days, range of dates?

public static class datefunctions {     public static datetime getpreviousinstanceofweekday(datetime fordate,          dayofweek dayofweek)     {         return fordate.dayofweek >= dayofweek              ? fordate.adddays(dayofweek - fordate.dayofweek).date              : fordate.adddays(-(7-(dayofweek - fordate.dayofweek))).date;     }      public static ienumerable<datetime> getdaterange(datetime startdate, int length)     {         return enumerable.range(0, length - 1).select(x => startdate.date.adddays(x));     } } 

the main reason sort of thing don't expect right on first try, , easiest way verify few unit tests.

[testmethod] public void returnspreviousmondayforgiventhursday() {     var monday = datefunctions.getpreviousinstanceofweekday(datetime.parse("4/21/2016"),          dayofweek.monday);     assert.areequal(datetime.parse("4/18/2016"), monday); }  [testmethod] public void returnsprevioussaturdayforgivensunday() {     var saturday = datefunctions.getpreviousinstanceofweekday(datetime.parse("4/17/2016"),          dayofweek.saturday);     assert.areequal(datetime.parse("4/16/2016"), saturday); }  [testmethod] public void returnssundayforgivensunday() {     var sunday = datefunctions.getpreviousinstanceofweekday(datetime.parse("4/24/2016"),          dayofweek.sunday);     assert.areequal(datetime.parse("4/24/2016"), sunday); } 

that keeps wednesday/6pm check separate rest. function combines other functions, , new thing introduces logic check whether should return 5 days starting previous monday or next monday. (i might put in separate function. few lines of code , unit test or 2 great peace of mind.)

public static ienumerable<datetime> getweekdaysforcurrentornextweek(datetime fordate) {     //badly named variable. it's late.     var getstartforweek = fordate.addhours(6).dayofweek > dayofweek.wednesday         ? fordate.adddays(7) : fordate;     return         datefunctions.getdaterange(         getpreviousinstanceofweekday(getstartforweek, dayofweek.monday), 5); } 

Comments

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -