- A+
所属分类:.NET技术
网上找过很多给一个时间段划分的功能,但是都感觉不是特别全也不是特别好用,索性自己根据网上的资料参考自己写了一个。
可以根据星期、月份、季度、年划分时间段(是严格按照起止时间划分的,例如:开始时间 2020-12-31 结束时间 2021-3-5,按月份划分就是 2020年12月份、2021年1月份、2021年2月份、2021年3月份;季度和年也是如此,后续可以根据自己需要改动代码)。
废话少说,直接上代码
public static class DateSplitHelper { #region 根据周期类型获取时间段分组 杨福良 2021-4-15 10:25:12 /// <summary> /// 根据周期类型获取时间段分组 /// </summary> /// <param name="startDate"></param> /// <param name="endDate"></param> /// <param name="caredCycle"></param> /// <returns></returns> public static Dictionary<string, string> GetDates(DateTime startDate, DateTime endDate, int caredCycle) { Dictionary<string, string> dates = new Dictionary<string, string>(); if (caredCycle == 0) { dates = DateSplitHelper.GetGroupWeek(startDate.ToShortDateString(), endDate.ToShortDateString()); return dates; } if (caredCycle == 1) { dates = DateSplitHelper.GetGroupMonth(startDate.ToShortDateString(), endDate.ToShortDateString()); return dates; } if (caredCycle == 2) { dates = DateSplitHelper.GetGroupSeason(startDate.ToShortDateString(), endDate.ToShortDateString()); return dates; } if (caredCycle == 3) { dates = DateSplitHelper.GetGroupYear(startDate.ToShortDateString(), endDate.ToShortDateString()); return dates; } return dates; } #endregion #region 根据时间范围获取每周分组 杨福良 2021-4-14 21:49:46 /// <summary> /// 根据时间范围获取每周分组 /// </summary> /// <param name="strStartDate">起始时间</param> /// <param name="strEndDate">结束时间</param> /// <returns>返回每周起始结束键值对</returns> static DateTime LastDayOfMonth(DateTime datetime) { return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1); } #endregion #region 获取当前季度 杨福良 2021-4-25 18:02:44 /// <summary> /// 获取当前季度 /// </summary> /// <param name="date"></param> /// <returns></returns> public static string GetMonthSeason(this string date) { DateTime dt = Convert.ToDateTime(date); int month = dt.Month; if (month <= 3) { return $"{dt.Year}年1季度"; } if (month <= 6 && month >= 4) { return $"{dt.Year}年2季度"; } if (month <= 9 && month >= 7) { return $"{dt.Year}年3季度"; } return $"{dt.Year}年4季度"; } #endregion /// <summary> /// 日期转换 /// </summary> /// <param name="date"></param> /// <param name="caredCycle">统计周期 0 按周 1 按月 2 按季度 3 按年</param> /// <returns></returns> public static string DateTransform(string startDate, string endDate, int caredCycle) { StringBuilder strSql = new StringBuilder(); if (caredCycle == 0) { strSql.Append($@" '{startDate}-{endDate}'AS AreaSection, "); } if (caredCycle == 1) { strSql.Append($@" '{Convert.ToDateTime(startDate).GetDateTimeFormats('y')[0].ToString()}'AS AreaSection,"); } if (caredCycle == 2) { strSql.Append($@" '{startDate.GetMonthSeason()}'AS AreaSection,"); } if (caredCycle == 3) { strSql.Append($@" '{Convert.ToDateTime(startDate).Year}年'AS AreaSection,"); } return strSql.ToString(); } static Dictionary<string, string> GetGroupWeek(string strStartDate, string strEndDate) { DateTime dtStartDate = DateTime.Parse(strStartDate); DateTime dtEndDate = DateTime.Parse(strEndDate); Dictionary<string, string> dict = new Dictionary<string, string>(); bool ok = true; while (ok) { DayOfWeek day = dtStartDate.DayOfWeek; string dayString = day.ToString(); DateTime dtTempStartDate = dtStartDate; DateTime dtTempDate = DateTime.Now; 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; } if (dtTempDate > dtEndDate) { dict.Add(dtTempStartDate.ToString(), dtEndDate.ToString()); ok = false; continue; } dict.Add(dtTempStartDate.ToString(), dtTempDate.ToString()); dtStartDate = dtTempDate.Date.AddDays(+1); if (dtStartDate > dtEndDate) { ok = false; } } return dict; } static Dictionary<string, string> GetGroupMonth(string strStartDate, string strEndDate) { DateTime dtStartDate = DateTime.Parse(strStartDate); DateTime dtEndDate = DateTime.Parse(strEndDate); Dictionary<string, string> dict = new Dictionary<string, string>(); bool ok = true; while (ok) { DateTime dtTempStartDate = dtStartDate; DateTime dtTempDate = LastDayOfMonth(dtStartDate); if (dtTempDate > dtEndDate) { dtTempDate = dtEndDate; ok = false; } dict.Add(dtTempStartDate.ToString(), dtTempDate.ToString()); dtStartDate = dtTempDate.Date.AddDays(+1); if (dtStartDate > dtEndDate) ok = false; //if (dtTempDate > dtEndDate) //{ // dict.Add(dtTempStartDate.ToString(), dtEndDate.ToString()); // ok = false; // continue; //} //dict.Add(dtTempStartDate.ToString(), dtTempDate.ToString()); //dtStartDate = dtTempDate.Date.AddDays(+1); } return dict; } static Dictionary<string, string> GetGroupSeason(string strStartDate, string strEndDate) { DateTime dtStartDate = DateTime.Parse(strStartDate); DateTime dtEndDate = DateTime.Parse(strEndDate); DateTime dtTempStartDate = dtStartDate; DateTime dtTempDate = DateTime.Now; Dictionary<string, string> dict = new Dictionary<string, string>(); bool ok = true; while (ok) { int currentMonth = dtStartDate.Month; int currentSeason = currentMonth / 3 + (currentMonth % 3 > 0 ? 1 : 0); dtTempDate = DateTime.Parse(DateTime.Parse(dtStartDate.Year + "." + (currentSeason * 3) + "." + "01").ToString("yyyy.MM.dd")); dtTempDate = LastDayOfMonth(dtTempDate); if (dtTempDate > dtEndDate) { dtTempDate = dtEndDate; ok = false; } dict.Add(dtTempStartDate.ToString(), dtTempDate.ToString()); dtStartDate = dtTempDate.Date.AddDays(+1); if (dtStartDate > dtEndDate) { dtTempDate = dtEndDate; ok = false; } dtTempStartDate = dtTempDate.Date.AddDays(+1); } return dict; } static Dictionary<string, string> GetGroupYear(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) { dict.Add(dtStartDate.ToShortDateString(), new DateTime(dtStartDate.Year + 1, 1, 1).AddDays(-1).ToShortDateString()); return dict; } bool ok = true; while (ok) { DateTime dtTempStartDate = dtStartDate; DateTime dtTempDate = DateTime.Parse(DateTime.Parse(dtStartDate.Year + ".12." + "31").ToString("yyyy.MM.dd")); if (dtTempDate > dtEndDate) { dtTempDate = dtEndDate; ok = false; } dict.Add(dtTempStartDate.ToString(), dtTempDate.ToString()); dtStartDate = dtTempDate.Date.AddDays(+1); if (dtStartDate > dtEndDate) { dtTempDate = dtEndDate; ok = false; } } return dict; } }
大家有疑问的地方可以随时沟通,非常乐意哦~~~