首页 > 代码库 > 当前时间+工作日 得到截止日期

当前时间+工作日 得到截止日期

客户希望实现计算工作日,除去公休日、节假日,并且规定的公休日上班要算作工作日。

如:2014-06-25 是礼拜三加上3天,是2014-06-28礼拜六。那么得到的时间应该是2014-06-30。

1、后台调用:

1                 int days = 3;//累计加的天数2                 DateTime add_date = DateTime.Now;3                 EndDate enddate = new EndDate();4                 count_date = enddate.Get_Days(add_date, days, 0);
Code

2、实现方法:

  1         /// <summary>  2         /// 计算当天是否为公休公休  3         /// </summary>  4         /// <param name="time"></param>  5         /// <returns></returns>  6         public static int GetPublicHoliday(DateTime time)  7         {  8             DBhelp db = new DBhelp();  9             int gongXiuTianShu = 0; 10             string endWeek = ""; 11  12             DataTable listDayRest = db.getTable("select * from L_day_rest where rest_type=‘3‘");//公休日,上班 13             if (listDayRest.Rows.Count > 0) 14             { 15                 for (int i = 0; i < listDayRest.Rows.Count; i++) 16                 { 17                     if (listDayRest.Rows[i]["start_date"] != null && listDayRest.Rows[i]["start_date"].ToString() != null) 18                     { 19                         if (time.ToShortDateString() == Convert.ToDateTime(listDayRest.Rows[i]["start_date"].ToString()).ToShortDateString()) 20                         { 21                             return gongXiuTianShu; 22                         } 23                     } 24                 } 25             } 26  27             //转换成中文星期几 28             switch (time.DayOfWeek.ToString()) 29             { 30                 case "Sunday": endWeek = "星期天"; break; 31                 case "Monday": endWeek = "星期一"; break; 32                 case "Tuesday": endWeek = "星期二"; break; 33                 case "Wednesday": endWeek = "星期三"; break; 34                 case "Thursday": endWeek = "星期四"; break; 35                 case "Friday": endWeek = "星期五"; break; 36                 case "Saturday": endWeek = "星期六"; break; 37             } 38             listDayRest = db.getTable("select * from L_day_rest where rest_type=‘2‘");//获得法定节日 39             if (listDayRest.Rows.Count > 0) 40             { 41                 string GongXiu1 = listDayRest.Rows[0]["start_date"].ToString(); 42                 string GongXiu2 = listDayRest.Rows[0]["end_time"].ToString(); 43                 for (int i = 0; i < listDayRest.Rows.Count; i++)//循环遍历法定节日 44                 { 45                     if (time >= Convert.ToDateTime(listDayRest.Rows[0]["start_date"].ToString()) && time < Convert.ToDateTime(listDayRest.Rows[0]["end_time"].ToString())) 46                     { 47                         TimeSpan ts = Convert.ToDateTime(listDayRest.Rows[0]["end_time"].ToString()) - time; 48                         gongXiuTianShu = ts.Days + 1; 49                     } 50                     else if (time > Convert.ToDateTime(listDayRest.Rows[0]["start_date"].ToString()) && time <= Convert.ToDateTime(listDayRest.Rows[0]["end_time"].ToString())) 51                     { 52                         TimeSpan ts = Convert.ToDateTime(listDayRest.Rows[0]["end_time"].ToString()) - time; 53                         gongXiuTianShu = ts.Days + 1; 54                     } 55                     else if (time == Convert.ToDateTime(listDayRest.Rows[0]["start_date"].ToString()) && time == Convert.ToDateTime(listDayRest.Rows[0]["end_time"].ToString())) 56                     { 57                         gongXiuTianShu = 1; 58                     } 59  60                 } 61             } 62             if (gongXiuTianShu == 0 || listDayRest.Rows.Count == 0) 63             { 64                 listDayRest = db.getTable("select * from L_day_rest where rest_type=‘1‘");//获得公休日 65                 if (listDayRest.Rows.Count > 0) 66                 { 67                     string GongXiu1 = listDayRest.Rows[0]["start_date"].ToString().Trim(); 68                     string GongXiu2 = listDayRest.Rows[0]["end_time"].ToString().Trim(); 69                     bool isContact = IsContact(GongXiu1, GongXiu2); 70                     if (isContact)//公休日连续 71                     { 72                         if (endWeek == GongXiu1) 73                         { 74                             gongXiuTianShu = 2; 75                         } 76                         else if (endWeek == GongXiu2) 77                         { 78                             gongXiuTianShu = 1; 79                         } 80                     } 81                     else//公休日不连续 82                     { 83                         if (endWeek == GongXiu1 || endWeek == GongXiu2) 84                         { 85                             gongXiuTianShu = 1; 86                         } 87                     } 88                 } 89             } 90  91             return gongXiuTianShu; 92         } 93  94         /// <summary> 95         /// 递归调用,计算天数 96         /// </summary> 97         /// <param name="time"></param> 98         /// <param name="days"></param> 99         /// <param name="date_days"></param>100         /// <returns></returns>101         public int Get_Days(DateTime time, int days, int date_days)102         {103             int gongXiuTianShu = 0;104             //int date_days = 0;105             int counts = 0;106             for (int i = 1; i <= days; i++)107             {108                 counts = i;109                 DateTime dates = time;110                 gongXiuTianShu = GetPublicHoliday(dates.AddDays(i));111 112                 if (gongXiuTianShu > 0 || counts == days)113                 {114                     date_days = date_days + gongXiuTianShu + i;115                     break;116                 }117 118             }119 120             if (counts > 0 && counts < days)121             {122                 return Get_Days(time.AddDays(date_days), days - counts, date_days);123             }124             else125             {126                 return date_days;127             }128         }129 130         /// <summary>131         /// 公休时间132         /// </summary>133         /// <returns></returns>134         private static bool IsContact(string start, string end)135         {136             bool flag = false;//定义标示137             switch (start)138             {139                 case "星期天":140                     if (end == "星期一")141                     {142                         flag = true;143                     } break;144                 case "星期一":145                     if (end == "星期二")146                     {147                         flag = true;148                     } break;149                 case "星期二":150                     if (end == "星期三")151                     {152                         flag = true;153                     } break;154                 case "星期三":155                     if (end == "星期四")156                     {157                         flag = true;158                     } break;159                 case "星期四":160                     if (end == "星期五")161                     {162                         flag = true;163                     } break;164                 case "星期五":165                     if (end == "星期六")166                     {167                         flag = true;168                     } break;169                 case "星期六":170                     if (end == "星期天")171                     {172                         flag = true;173                     } break;174             }175             return flag;176         }
Code

3、数据库表结构:

 

注:以上只是实现了基本功能,并且效率不高,因水平有限,希望能有更好的方法。