绝对原创,判断条件比较多,考虑到午休情况,喜欢的请拿走。。。
/// <summary> /// 计算今天有效工作小时数 /// </summary> public static Decimal GetWorkHour(CheckOnModel todayCheckOn) { //计算今天有效工作小时数 //正常情况上下班 早上早与9:00 下班晚于18:00 DateTime amTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 09:01"); DateTime pmTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 18:00"); DateTime noonBreakStartTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 12:30"); DateTime noonBreakEndTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 13:30"); CheckOnModel copyCheckOn = CheckOnModel.DeepCopy(todayCheckOn); //扣除一个小时午休 bool subtractOneHour = true; //早于9:05分上班 上班打卡时间从9点起算 if (todayCheckOn.OnDutyTime.Value <= amTime) { copyCheckOn.OnDutyTime = amTime.AddMinutes(-1); } //在午休时间上班 从午休结束时开始算 if (todayCheckOn.OnDutyTime.Value >= noonBreakStartTime && todayCheckOn.OnDutyTime.Value <= noonBreakEndTime) { copyCheckOn.OnDutyTime = noonBreakEndTime; subtractOneHour = false;//不再扣除午休时间 } //从午休结束后开始上班 if (todayCheckOn.OnDutyTime.Value >= noonBreakEndTime) { subtractOneHour = false;//不再扣除午休时间 } //下班打卡时间从18点起算 if (todayCheckOn.OffDutyTime.Value >= pmTime) { copyCheckOn.OffDutyTime = pmTime; } //下班时间介于午休时间 if (copyCheckOn.OffDutyTime.Value >= noonBreakStartTime && copyCheckOn.OffDutyTime.Value < noonBreakEndTime) { copyCheckOn.OffDutyTime = noonBreakStartTime; subtractOneHour = false;//不再扣除午休时间 } //下班时间早于午休时间 if (copyCheckOn.OffDutyTime.Value < noonBreakStartTime) { subtractOneHour = false;//不再扣除午休时间 } if (subtractOneHour) { copyCheckOn.WorkHour = Convert.ToDecimal((copyCheckOn.OffDutyTime.Value - copyCheckOn.OnDutyTime.Value).TotalHours - 1); } else { copyCheckOn.WorkHour = Convert.ToDecimal((copyCheckOn.OffDutyTime.Value - copyCheckOn.OnDutyTime.Value).TotalHours); } //午休时间打卡走人的,可能为负数 if (copyCheckOn.WorkHour < 0) { copyCheckOn.WorkHour = 0; } //补签成功的 视为正常打卡 但要减去扣除小时数 if (copyCheckOn.Status == "Y" && copyCheckOn.EditMan != new Guid()) { copyCheckOn.WorkHour = 8 - copyCheckOn.SubtractHour; } return copyCheckOn.WorkHour; }