/// <summary> /// 判断是否为工作日 /// </summary> /// <param name="currDate">要判断的时间</param> /// <param name="thisYearData">当前的数据</param> /// <returns></returns> private bool IsWorkDay(DateTime currDate, DateModel thisYearData) { if (currDate.Year != thisYearData.Year)//跨年重新读取数据 { thisYearData = GetConfigDataByYear(currDate.Year); } if (thisYearData.Year > 0) { string date = currDate.ToString("MMdd"); int week = (int)currDate.DayOfWeek; if (thisYearData.Work.IndexOf(date) >= 0) { return(true); } if (thisYearData.Holiday.IndexOf(date) >= 0) { return(false); } if (week != 0 && week != 6) { return(true); } } return(false); }
/// <summary> /// 根据传入的时间,计算工作日天数 /// </summary> /// <param name="date">带计算的时间</param> /// <param name="isContainToday">当天是否算工作日(默认:true)</param> /// <returns></returns> public int GetWorkDayNum(DateTime date, bool isContainToday = true) { var currDate = DateTime.Now; int workDayNum = 0; int addDay = date.Date > currDate.Date ? 1 : -1; if (isContainToday) { currDate = currDate.AddDays(-addDay); } DateModel thisYearData = GetConfigDataByYear(currDate.Year); if (thisYearData.Year > 0) { bool isEnd = false; do { currDate = currDate.AddDays(addDay); if (IsWorkDay(currDate, thisYearData)) { workDayNum += addDay; } isEnd = addDay > 0 ? (date.Date > currDate.Date) : (date.Date < currDate.Date); } while (isEnd); } return(workDayNum); }
/// <summary> /// 根据传入的工作日天数,获得计算后的日期,可传负数 /// </summary> /// <param name="day">天数</param> /// <param name="isContainToday">当天是否算工作日(默认:true)</param> /// <returns></returns> public DateTime GetReckonDate(int day, bool isContainToday = true) { DateTime currDate = DateTime.Now; int addDay = day >= 0 ? 1 : -1; if (isContainToday) { currDate = currDate.AddDays(-addDay); } DateModel thisYearData = GetConfigDataByYear(currDate.Year); if (thisYearData.Year > 0) { int sumDay = Math.Abs(day); int workDayNum = 0; while (workDayNum < sumDay) { currDate = currDate.AddDays(addDay); if (IsWorkDay(currDate, thisYearData)) { workDayNum++; } } } return(currDate); }
/// <summary> /// 获取指定年份的数据 /// </summary> /// <param name="year"></param> /// <returns></returns> private DateModel GetConfigDataByYear(int year) { if (cacheDateList == null)//取配置数据 { GetConfigList(); } DateModel result = cacheDateList.FirstOrDefault(m => m.Year == year); return(result); }