1.
#region 日期处理函数 /// <summary> /// 计算本周的周一日期 /// </summary> /// <returns></returns> private static DateTime GetMondayDate() { return GetMondayDate(DateTime.Now); } /// <summary> /// 计算本周周日的日期 /// </summary> /// <returns></returns> private static DateTime GetSundayDate() { return GetSundayDate(DateTime.Now); } /// <summary> /// 计算某日起始日期(礼拜一的日期) /// </summary> /// <param name="someDate">该周中任意一天</param> /// <returns>返回礼拜一日期,后面的具体时、分、秒和传入值相等</returns> private static DateTime GetMondayDate(DateTime someDate) { int i = someDate.DayOfWeek - DayOfWeek.Monday; if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Subtract(ts); } /// <summary> /// 计算某日起始日期(礼拜六的日期) /// </summary> /// <param name="someDate">该周中任意一天</param> /// <returns>返回礼拜一日期,后面的具体时、分、秒和传入值相等</returns> private static DateTime GetSaturdayDate(DateTime someDate) { int i = someDate.DayOfWeek - DayOfWeek.Saturday; if (i == -6) i = 1;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Saturday=-6,必须+7=1。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Subtract(ts); } /// <summary> /// 计算某日结束日期(礼拜日的日期) /// </summary> /// <param name="someDate">该周中任意一天</param> /// <returns>返回礼拜日日期,后面的具体时、分、秒和传入值相等</returns> private static DateTime GetSundayDate(DateTime someDate) { int i = someDate.DayOfWeek - DayOfWeek.Sunday; if (i != 0) i = 7 - i;// 因为枚举原因,Sunday排在最前,相减间隔要被7减。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Add(ts); } /// <summary> /// 取得某月的第一天 /// </summary> /// <param name="datetime">该月的任意一天</param> /// <returns></returns> private static DateTime FirstDayOfMonth(DateTime datetime) { return datetime.AddDays(1 - datetime.Day); } //// <summary> /// 取得某月的最后一天 /// </summary> /// <param name="datetime">该月的任意一天</param> /// <returns></returns> private static DateTime LastDayOfMonth(DateTime datetime) { return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1); } /// <summary> /// 取得某月的所有周末日期 /// </summary> /// <param name="datetime">该月的任意一天</param> /// <returns></returns> private static List<DateTime> WeekendOfMonth(DateTime datetime) { List<DateTime> weekenList = new List<DateTime>(); DateTime fM = FirstDayOfMonth(datetime); DateTime lM = LastDayOfMonth(datetime); int length = lM.Day; for (int i = 0; i <= length; i++) { if (fM.AddDays(i) <= lM && (fM.DayOfWeek == DayOfWeek.Saturday || fM.DayOfWeek == DayOfWeek.Sunday)) { weekenList.Add(fM); } } return weekenList; } #endregion