首页 > 代码库 > C#根据时间范围获取每月每周的分组
C#根据时间范围获取每月每周的分组
C#根据时间范围获取每月每周的分组
简介:C#根据时间范围获取每月每周的分组,这个时间范围可以是多年(2012.01.01-2015.12.31),
也可以是一年中的几个月(2015.01.01-2015.12.31),也可以是月个月中的几个星期
(2015.01.01-2015.01.21),根据起始日期和结束日期,生成一个以起始日期和结束日期为
键值对的字典(Dictionary<string, string> ),时间仓促,初步写的算法,以后再优化,目前
唯一的一点小遗憾就是,月中每周键值对的排序问题。详细代码如下:
-------------------------------------------------------------------------------------------------------------------------
根据时间范围获取每月每周的分组:
/// <summary> /// 根据时间范围获取每月每周的分组 /// </summary> /// <param name="strStartDate">起始时间</param> /// <param name="strEndDate">结束时间</param> /// <returns>返回每周起始结束键值对</returns> /// <remarks>创建人员(日期):★彭振★(150106 11:12)</remarks> public static Dictionary<string, string> GetGroupWeekByDateRange(string strStartDate, string strEndDate) { Dictionary<string, string> dict = new Dictionary<string, string>(); DateTime dtStartDate = DateTime.Parse(strStartDate); DateTime dtEndDate = DateTime.Parse(strEndDate); //同年 if (dtStartDate.Year == dtEndDate.Year) { GetGroupWeekByYear(dict, dtStartDate, dtEndDate); } //不同年 else { int WhileCount = dtEndDate.Year - dtStartDate.Year; //某年一共有多少天 int YearDay = DateTime.IsLeapYear(dtStartDate.Year) ? 366 : 365; DateTime dtTempStartDate = dtStartDate; DateTime dtTempEndDate = dtTempStartDate.AddDays(YearDay - dtTempStartDate.DayOfYear); //根据时间范围获取每月每周的分组 GetGroupWeekByYear(dict, dtTempStartDate, dtTempEndDate); for (int i = 1; i < (WhileCount + 1); i++) { //某年某月一共有多少天 YearDay = DateTime.IsLeapYear(dtTempStartDate.Year + 1) ? 366 : 365; dtTempStartDate = DateTime.Parse(DateTime.Parse((dtTempStartDate.Year + 1) + "." + dtTempStartDate.Month + "." + "01").ToString("yyyy.MM.dd")); dtTempEndDate = dtTempStartDate.AddDays(YearDay - dtTempStartDate.DayOfYear); //根据时间范围获取每月每周的分组 GetGroupWeekByYear(dict, dtTempStartDate, dtTempEndDate); } } return dict; }
-------------------------------------------------------------------------------------------------------------------------
根据时间范围(年)获取每月每周的分组:
/// <summary> /// 根据时间范围(年)获取每月每周的分组 /// </summary> /// <param name="dict">每周起始结束键值对</param> /// <param name="strStartDate">起始时间</param> /// <param name="strEndDate">结束时间</param> /// <remarks>创建人员(日期):★彭振★(150106 13:58)</remarks> public static void GetGroupWeekByYear(Dictionary<string, string> dict, DateTime dtStartDate, DateTime dtEndDate) { //不同月 if ((dtEndDate.Month - dtStartDate.Month) >= 1) { int WhileCount = dtEndDate.Month - dtStartDate.Month; //某年某月一共有多少天 int MonthDay = DateTime.DaysInMonth(dtStartDate.Year, dtStartDate.Month); DateTime dtTempStartDate = dtStartDate; DateTime dtTempEndDate = dtTempStartDate.AddDays(MonthDay - 1); //根据时间范围获取每月每周的分组 GetGroupWeekByMonth(dict, dtTempStartDate, dtTempEndDate); for (int i = 1; i < (WhileCount + 1); i++) { //某年某月一共有多少天 MonthDay = DateTime.DaysInMonth(dtTempStartDate.Year, dtTempStartDate.Month + 1); dtTempStartDate = DateTime.Parse(DateTime.Parse(dtTempStartDate.Year + "." + (dtTempStartDate.Month + 1) + "." + "01").ToString("yyyy.MM.dd")); dtTempEndDate = dtTempStartDate.AddDays(MonthDay - 1); //根据时间范围获取每月每周的分组 GetGroupWeekByMonth(dict, dtTempStartDate, dtTempEndDate); } } //同月 else { //根据时间范围获取每月每周的分组 GetGroupWeekByMonth(dict, dtStartDate, dtEndDate); } }
-------------------------------------------------------------------------------------------------------------------------
根据时间范围(月)获取每月每周的分组
/// <summary> /// 根据时间范围(月)获取每月每周的分组 /// </summary> /// <param name="dict">每周起始结束键值对</param> /// <param name="strStartDate">起始时间</param> /// <param name="strEndDate">结束时间</param> /// <remarks>创建人员(日期):★彭振★(150106 11:13)</remarks> public static void GetGroupWeekByMonth(Dictionary<string, string> dict, DateTime dtStartDate, DateTime dtEndDate) { //一周 if ((dtEndDate.Day - dtStartDate.Day) < 7) { DayOfWeek day = dtStartDate.DayOfWeek; string dayString = day.ToString(); DateTime dtTempStartDate = dtStartDate; DateTime dtTempEndDate = dtEndDate; DateTime dtTempDate = DateTime.Now; switch (dayString) { case "Monday": dict.Add(dtTempStartDate.ToString(), dtTempEndDate.ToString()); break; case "Tuesday": dtTempDate = dtTempStartDate.Date.AddDays(+5); break; case "Wednesday": dtTempDate = dtTempStartDate.Date.AddDays(+4); break; case "Thursday": dtTempDate = dtTempStartDate.Date.AddDays(+3); break; case "Friday": dtTempDate = dtTempStartDate.Date.AddDays(+2); break; case "Saturday": dtTempDate = dtTempStartDate.Date.AddDays(+1); break; case "Sunday": dtTempDate = dtTempStartDate; break; } if (!dayString.Equals("Monday")) { dict.Add(dtTempStartDate.ToString(), dtTempDate.ToString()); dtTempDate = dtTempDate.Date.AddDays(+1); if (DateTime.Compare(dtTempDate, dtEndDate) <= 0) { dict.Add(dtTempDate.ToString(), dtTempEndDate.ToString()); } } } //多周 else { DayOfWeek day = dtStartDate.DayOfWeek; string dayString = day.ToString(); DateTime dtTempStartDate = dtStartDate; DateTime dtTempEndDate = dtEndDate; DateTime dtTempDate = DateTime.Now; #region 起始 switch (dayString) { case "Monday": dtTempDate = dtTempStartDate.Date.AddDays(+6); break; case "Tuesday": dtTempDate = dtTempStartDate.Date.AddDays(+5); break; case "Wednesday": dtTempDate = dtTempStartDate.Date.AddDays(+4); break; case "Thursday": dtTempDate = dtTempStartDate.Date.AddDays(+3); break; case "Friday": dtTempDate = dtTempStartDate.Date.AddDays(+2); break; case "Saturday": dtTempDate = dtTempStartDate.Date.AddDays(+1); break; case "Sunday": dtTempDate = dtTempStartDate; break; } dict.Add(dtTempStartDate.ToString(), dtTempDate.ToString()); dtTempStartDate = dtTempDate.Date.AddDays(+1); #endregion #region 结束 day = dtEndDate.DayOfWeek; dayString = day.ToString(); switch (dayString) { case "Monday": dtTempDate = dtEndDate; break; case "Tuesday": dtTempDate = dtEndDate.Date.AddDays(-1); break; case "Wednesday": dtTempDate = dtEndDate.Date.AddDays(-2); break; case "Thursday": dtTempDate = dtEndDate.Date.AddDays(-3); break; case "Friday": dtTempDate = dtEndDate.Date.AddDays(-4); break; case "Saturday": dtTempDate = dtEndDate.Date.AddDays(-5); break; case "Sunday": dtTempDate = dtEndDate.Date.AddDays(-6); break; } dict.Add(dtTempDate.ToString(), dtEndDate.ToString()); dtTempEndDate = dtTempDate.Date.AddDays(-1); #endregion int WhileCount = ((dtTempEndDate.Day - dtTempStartDate.Day) / 7); if (WhileCount == 0) { dict.Add(dtTempStartDate.ToString(), dtTempEndDate.ToString()); } else { for (int i = 0; i < (WhileCount + 1); i++) { dtTempDate = dtTempStartDate.Date.AddDays(+6); dict.Add(dtTempStartDate.ToString(), dtTempDate.ToString()); dtTempStartDate = dtTempDate.Date.AddDays(+1); ; } } } }
-------------------------------------------------------------------------------------------------------------------------
运用:
//根据时间范围获取每月每周的分组 Dictionary<string, string> dict = GetGroupWeekByDateRange("2012.01.01", "2015.12.31"); Dictionary<string, string> dict = GetGroupWeekByDateRange("2015.01.01", "2015.12.31"); Dictionary<string, string> dict = GetGroupWeekByDateRange("2015.01.01", "2015.01.21");
C#根据时间范围获取每月每周的分组