计算两个时间之内的工作时长:
public static List<LaborTimeInfo> AllTimes(string fromDate ,string toDate) { DateTime f = DateTime.Parse(fromDate); DateTime t = DateTime.Parse(toDate); List<LaborTimeInfo> laborTimes = new List<LaborTimeInfo>(); List<LaborTimeInfo> laborFrom = new List<LaborTimeInfo>(); List<LaborTimeInfo> laborMiddle = new List<LaborTimeInfo>(); List<LaborTimeInfo> laborTo = new List<LaborTimeInfo>(); List<WorkingTimeListInfo> ws = new List<WorkingTimeListInfo>(); WorkingTimeListInfo nomal = new WorkingTimeListInfo(); ws = ListWorkingTime("", "", "1", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""); foreach (var item in ws)//找到正常上下班时间比如:朝九晚五 { if (item.startHour!=0&&item.endHour!=0) { nomal = item; } } if (nomal.workingTimeId<=0)//如果未定义那么就按朝九晚五计算 { nomal.startHour = 9; nomal.startMinute = 0; nomal.endHour = 17; nomal.endMinute = 0; } TimeSpan ts = t.Subtract(f);//时间差 if (ts.Days < 0) { return laborTimes; } else if (ts.Days==0)//如果开始和计算是同一天 { laborFrom = CalLaborHours(f.ToString(), f.Hour.ToString(), f.Minute.ToString(), t.Hour.ToString(), t.Minute.ToString()); } else { laborFrom = CalLaborHours(f.ToString(), f.Hour.ToString(), f.Minute.ToString(), nomal.endHour.ToString(), nomal.endMinute.ToString()); for (int i = 1; i < ts.Days; i++)//两天之间的每一天都要去计算工作时长 { laborMiddle = CalLaborHours(f.AddDays(i).ToString(), nomal.startHour.ToString(), nomal.startMinute.ToString(), nomal.endHour.ToString(), nomal.endMinute.ToString()); foreach (var from in laborFrom)//循环把中间天的时长加起来,中间的都按照定义的正常的上班时间 { foreach (var mid in laborMiddle) { if (from.laborType == mid.laborType) { from.hours = from.hours + mid.hours; } } } } laborTo = CalLaborHours(t.ToString(), nomal.startHour.ToString(), nomal.startMinute.ToString(), t.Hour.ToString(), t.Minute.ToString()); foreach (var from in laborFrom)//最后一天的时长,并加起来 { foreach (var to in laborTo) { if (from.laborType==to.laborType) { from.hours=from.hours + to.hours; } } } } laborTimes = laborFrom; return laborTimes; } //Added public static List<LaborTimeInfo> CalLaborHours(string date, string startHour, string startMinute, string endHour, string endMinute) {//计算每一天的工作时长 string laborType = ""; int istartHour = 0; int istartMinute = 0; int iendHour = 0; int iendMinute = 0; decimal totalHours = 0; string year = ""; string month = ""; string day = ""; string dayOfWeek; bool flag = false;//判断当天是不是假期或者周末时用。如果是周末,则不加入到正常时长 WorkingTimeListInfo w = new WorkingTimeListInfo(); WorkingTimeListInfo w2 = new WorkingTimeListInfo(); List<WorkingTimeListInfo> ws = new List<WorkingTimeListInfo>(); LaborTimeInfo laborTime = new LaborTimeInfo(); LaborTimeInfo laborTime2 = new LaborTimeInfo(); LaborTimeInfo laborTime3 = new LaborTimeInfo(); LaborTimeInfo laborTime4 = new LaborTimeInfo(); List<LaborTimeInfo> laborTimes = new List<LaborTimeInfo>(); List<LaborTimeInfo> normalLaborTimes = new List<LaborTimeInfo>(); StringBuilder strSQL = new StringBuilder(); if (String.IsNullOrEmpty(date)) { return laborTimes; } DateTime dt = DateTime.Parse(date); year = dt.Year.ToString(); month = dt.Month.ToString(); day = dt.Day.ToString(); dayOfWeek = dt.DayOfWeek.ToString(); if ((String.IsNullOrEmpty(startHour) == true) || (String.IsNullOrEmpty(endHour) == true)) return laborTimes; if (String.IsNullOrEmpty(startMinute) == true) startMinute = "0"; if (String.IsNullOrEmpty(endMinute) == true) endMinute = "0"; istartHour = int.Parse(startHour); istartMinute = int.Parse(startMinute); iendHour = int.Parse(endHour); iendMinute = int.Parse(endMinute); if ((istartHour < 0) || (istartHour > 24)) return laborTimes; if ((iendHour < 0) || (iendHour > 24)) return laborTimes; if ((istartMinute < 0) || (istartMinute > 59)) return laborTimes; if ((iendMinute < 0) || (iendMinute > 59)) return laborTimes; totalHours = iendHour - istartHour + decimal.Round(new decimal(iendMinute - istartMinute) / 60, 2);//计算当天的工作总时间 if (totalHours <= 0) return laborTimes; w = SearchHoliday(year,month, day);//判断是不是假期 if (w.workingTimeId > 0) { flag = true; laborTime.laborType = w.laborType; laborTime.date = date; laborTime.dayOfWeek = dayOfWeek; laborTime.hours = totalHours; laborTimes.Add(laborTime); //return laborTimes; } else { laborTime.laborType = "4"; laborTime.date = date; laborTime.dayOfWeek = dayOfWeek; laborTime.hours = 0; laborTimes.Add(laborTime); } w2 = SearchWeekEnd(year,month, day, dayOfWeek);//判断是不是周末(判断这天是不是调休的) if (w2.workingTimeId > 0)//是周末并且不是调休 { flag = true; laborTime2.laborType = w2.laborType; laborTime2.date = date; laborTime2.dayOfWeek = dayOfWeek; laborTime2.hours = totalHours; laborTimes.Add(laborTime2); //return laborTimes; } else { laborTime2.laborType ="3"; laborTime2.date = date; laborTime2.dayOfWeek = dayOfWeek; laborTime2.hours = 0; laborTimes.Add(laborTime2); } if (flag)//如果是周末或者节假日的话,正常的时长置0 { laborTime3.laborType = "2"; laborTime3.date = date; laborTime3.dayOfWeek = dayOfWeek; laborTime3.hours = 0; laborTimes.Add(laborTime3); laborTime4.laborType = "1"; laborTime4.date = date; laborTime4.dayOfWeek = dayOfWeek; laborTime4.hours = 0; laborTimes.Add(laborTime4); } else { laborType = "2"; //正常加班 laborTimes.AddRange(CalNormalLaborHours(date, laborType, startHour, startMinute, endHour, endMinute)); laborType = "1"; //正常工作 normalLaborTimes = CalNormalLaborHours(date, laborType, startHour, startMinute, endHour, endMinute); laborTimes.AddRange(normalLaborTimes); } return laborTimes; }