c# - How to get the next working day, excluding weekends and holidays -


i have requirement need work on date field, requirement thing

i call field minimum possible date

  1. add +1 date

  2. if minimum possible date happens fall on weekend(sat or sun) after adding 1 day, display next working day i.e monday

  3. if the minimum possible date happens fall on holiday, display next working day. (holidays 1.1 , 1.5 , 3.10 , 25.12 , 26.12)

  4. if minimum possible date happens fall on weekend(sat or sun) after adding 1 day, , day after holiday show next working day. eg: after +1 day if min possible day saturday, have display monday. if monday happens holiday have display tuesday.

i have tried solution above problem having multiple if , else cases, wondering if there generic , graceful way of doing it?

i have tried

var holidays = new list<datetime>(); holidays.add(new datetime(datetime.now.year,1,1)); holidays.add(new datetime(datetime.now.year,1,5)); holidays.add(new datetime(datetime.now.year,3,10)); holidays.add(new datetime(datetime.now.year,12,25));  if(date.dayofweek === dayofweek.saturday || date.dayofweek === dayofweek.sunday)  {       //logic add +1 , again logic check weekends , weekdays }   else if(holidays.contain(date)) {     //logic add +1 , again logic check weekends , weekdays } 

basically want next working day. loop on condition adding 1 day current date

do {   date = date.adddays(1); } while(isholliday(date) || isweekend(date)); 

in previous code isholliday predicate telling if date holliday. instance, shamelessly reusing code:

class program {     private static readonly hashset<datetime> holidays = new hashset<datetime>();      private static bool isholiday(datetime date)     {         return holidays.contains(date);     }      private static bool isweekend(datetime date)     {         return date.dayofweek == dayofweek.saturday             || date.dayofweek == dayofweek.sunday;     }       private static datetime getnextworkingday(datetime date)     {                 {             date = date.adddays(1);         } while (isholiday(date) || isweekend(date));         return date;     }      static void main(string[] args)     {         holidays.add(new datetime(datetime.now.year, 1, 1));         holidays.add(new datetime(datetime.now.year, 1, 5));         holidays.add(new datetime(datetime.now.year, 3, 10));         holidays.add(new datetime(datetime.now.year, 12, 25));          var dt = getnextworkingday(datetime.parse(@"2015-10-31"));          console.writeline(dt);          console.readkey();     } } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -