c# - Is there an optimized way of getting business weeks between two dates? -


just wondering if there more optimized and/or neater way (using linq example) of writing have below list of business week date ranges between 2 dates?

this have ..

// storage public class bucket {     public datetime startdate { get; set; }     public datetime enddate { get; set; } }  // other code removed brevity ...  datetime start = new datetime(2015, 7, 1); datetime end = new datetime(2015, 9, 1);  dayofweek firstdayofweek = dayofweek.monday; dayofweek lastdayofweek = dayofweek.friday;  var buckets = new list<bucket>(); var currentdate = start; datetime startofbucket = currentdate; datetime endofbucket = currentdate; while (currentdate <= end) {     var currentdayofweek = currentdate.dayofweek;      // skip days outside business week     if (currentdayofweek >= firstdayofweek && currentdayofweek <= lastdayofweek)     {         if (currentdayofweek == firstdayofweek)         {             // start new bucket             startofbucket = currentdate;         }          if ((currentdayofweek == lastdayofweek) || (currentdate == end))         {             // end of bucket             endofbucket = currentdate;              // create bucket             buckets.add(new bucket()              {                  startdate = startofbucket,                  enddate = endofbucket              });          }     }      currentdate = currentdate.adddays(1); } 

and give me following date ranges ...

  • start: 01/jul/2015 end: 03/jul/2015
  • start: 06/jul/2015 end: 10/jul/2015
  • start: 13/jul/2015 end: 17/jul/2015
  • start: 20/jul/2015 end: 24/jul/2015
  • start: 27/jul/2015 end: 31/jul/2015
  • start: 03/aug/2015 end: 07/aug/2015
  • start: 10/aug/2015 end: 14/aug/2015
  • start: 17/aug/2015 end: 21/aug/2015
  • start: 24/aug/2015 end: 28/aug/2015
  • start: 31/aug/2015 end: 01/sep/2015

n.b. first , last weeks purposefully not full weeks (they abide date range given).

edit solution provided here gives number of days between 2 dates interested in getting collection of date ranges.

also, don't need account holidays.

thanks,

it's quite handy using linq

var startdate = new datetime(2015, 7, 1); var enddate = new datetime(2015, 9, 1); var workdates = enumerable.range(0, (int)(enddate - startdate).totaldays + 1)     .select(i => startdate.adddays(i))     .where(date => (date.dayofweek != dayofweek.saturday && date.dayofweek != dayofweek.sunday))     .select(i => i);   var display = workdates     .groupadjacentby((x, y) => x.adddays(1) == y)     .select(g => string.format("start: {0:dd/mmm/yyyy} end: {1:dd/mmm/yyyy}", g.first(), g.last())); 

with extension method groupadjacentby<t>

public static class ienumerableextension {     public static ienumerable<ienumerable<t>> groupadjacentby<t>(         ienumerable<t> source, func<t, t, bool> predicate)     {         using (var e = source.getenumerator())         {             if (e.movenext())             {                 var list = new list<t> { e.current };                 var pred = e.current;                 while (e.movenext())                 {                     if (predicate(pred, e.current))                     {                         list.add(e.current);                     }                     else                     {                         yield return list;                         list = new list<t> { e.current };                     }                     pred = e.current;                 }                 yield return list;             }         }     } } 

fiddle


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -