/// <summary> /// 判断两个日期是否在同一周 /// </summary> /// <param name="dtmS">开始日期</param> /// <param name="dtmE">结束日期</param> /// <returns></returns> private bool IsInSameWeek(DateTime dtmS, DateTime dtmE) { TimeSpan ts = dtmE - dtmS; double dbl = ts.TotalDays; int intDow = Convert.ToInt32(dtmE.DayOfWeek); if (intDow == 0) intDow = 7; if (dbl >= 7 || dbl >= intDow) return false; else return true; } /// <summary> /// 某日期是本月的第几周 /// </summary> /// <param name="dtSel"></param> /// <param name="sundayStart"></param> /// <returns></returns> private int WeekOfMonth(DateTime dtSel, bool sundayStart) { //如果要判断的日期为1号,则肯定是第一周了 if (dtSel.Day == 1) return 1; else { //得到本月第一天 DateTime dtStart = new DateTime(dtSel.Year, dtSel.Month, 1); //得到本月第一天是周几 int dayofweek = (int)dtStart.DayOfWeek; //如果不是以周日开始,需要重新计算一下dayofweek,详细风DayOfWeek枚举的定义 if (!sundayStart) { dayofweek = dayofweek - 1; if (dayofweek < 0) dayofweek = 7; } //得到本月的第一周一共有几天 int startWeekDays = 7 - dayofweek; //如果要判断的日期在第一周范围内,返回1 if (dtSel.Day <= startWeekDays) return 1; else { int aday = dtSel.Day + 7 - startWeekDays; return aday / 7 + (aday % 7 > 0 ? 1 : 0); } } }
时间: 2024-10-09 16:02:58