计算工作日

  • 2019 年 10 月 10 日
  • 筆記

计算指定工作日后的日期

第一种:

        public static DateTime GetWorkday(DateTime now, int days)          {              int week = (int)now.DayOfWeek;              int addday = days / 5 * 7 + days % 5;              if (week == 6)//如果是周六就加一天到星期天              {                  addday += 1;              }              else if ((week + days % 5) > 5)              {                  addday += 2;              }              return now.AddDays(addday);          }

第二种:

        public static DateTime GetWorkdayLoop(DateTime now, int days)          {              DateTime temp = now;              while (days-- > 0)              {                  temp = temp.AddDays(1);                  while (temp.DayOfWeek == System.DayOfWeek.Saturday || temp.DayOfWeek == System.DayOfWeek.Sunday)                      temp = temp.AddDays(1);              }              return temp;          }