asp.net mvc 4 - fortnightly C# logic -


anyone please me fortnightly c# mvc 5 calendar logic. i've implemented weekly dates.

this weekly code. working.

var day = (from o in db.suburbs.asenumerable()     ..etc     select new      {      dt.deliveryday, }).tolist();   foreach (var temp in day) {     if (type == "weekly")     {         int weeklylogic = ((int)enum.parse(typeof(dayofweek), temp.deliveryday) - (int)today.dayofweek + 7) % 7;         datetime  nextweeklylogic = today.adddays(weeklylogic);         firstweek = nextweeklylogic;         int weekly2 = (((int)enum.parse(typeof(dayofweek), temp.deliveryday) - (int)today.dayofweek + 7) % 7) + 7;         datetime secondweek = today.adddays(weekly2);         int weekly3 = (((int)enum.parse(typeof(dayofweek), temp.deliveryday) - (int)today.dayofweek + 7) % 7) + 14;         datetime thirdweek = today.adddays(weekly3);         etc..     }     else if (type == "fortnightly")     {      } } 

for example, type == "fortnightly", example temp.deliveryday == "friday", based on today (february 2nd), expect output be:

february 5th (first friday after today, since temp.deliveryday friday) february 19th (14 days or fortnight first output) march 4th etc.  

first thing - you're repeating of behavior weekly dates. i'd recommend decomposing operation have. see 2 main parts.

  • get date of first arbitrary day of week after today.
  • get periodic recurring date date determined above.

this method can take care of first part,

datetime firstdayofweekafter(datetime day, dayofweek dayofweek) {     // don't want return today.      day = day.adddays(1);      // if current tested day desired day of week, return.      if (day.dayofweek == dayofweek)     {         return day;     }     // else call again next day.     else     {         return firstdayofweekafter(day, dayofweek);     } } 

and pair of methods take care of second part. have first method here casting (dayofweek) isn't necessary when use it.

ienumerable<datetime> getperiodicdeliverydates(datetime today, object dayofdelivery, int period) {     return getperiodicdeliverydates(today, (dayofweek)dayofdelivery, period); }  ienumerable<datetime> getperiodicdeliverydates(datetime today, dayofweek dayofdelivery, int period) {     // date of first delivery.      var firstdelivery = firstdayofweekafter(today, dayofdelivery);      while (true)     {         // infinitely yield dates separated supplied period length.         yield return firstdelivery;         firstdelivery = firstdelivery.adddays(period);     } } 

usage of these methods straightforward. difference between weekly , fortnightly enumerables period passed in getperiodicdeliverydates(). want make sure not attempt enumerate on getperiodicdeliverydates() - it's in infinite sequence.

var today = datetime.today; var temp = new { deliveryday = "friday" };  var weekly = getperiodicdeliverydates(today, enum.parse(typeof(dayofweek), temp.deliveryday), 7); var fortnightly = getperiodicdeliverydates(today, enum.parse(typeof(dayofweek), temp.deliveryday), 14);  weekly.take(5).tolist().foreach(x => console.writeline(x)); console.writeline(); fortnightly.take(5).tolist().foreach(x => console.writeline(x)); 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -