C#根据时间范围获取每年每月每周的分组
2021-03-14 06:45
标签:一周 rem count art 一点 tostring datetime star article 简介:C#根据时间范围获取每年每月每周的分组,这个时间范围可以是多年(2012.01.01-2015.12.31), 也可以是一年中的几个月(2015.01.01-2015.12.31),也可以是月个月中的几个星期 (2015.01.01-2015.01.21),根据起始日期和结束日期,生成一个以起始日期和结束日期为 键值对的字典(Dictionary 唯一的一点小遗憾就是,月中每周键值对的排序问题。详细代码如下: ------------------------------------------------------------------------------------------------------------------------- 根据时间范围获取每年每月每周的分组: /// 根据时间范围(年)获取每月每周的分组: 根据时间范围(月)获取每月每周的分组 备注,自己修改的算法: public Dictionary DateTime dtTempStartDate = dtStartDate; switch (dayString) if (DateTime.Compare(dtEndDate, dtTempDate) { dtTempStartDate = dtTempDate.Date.AddDays(+1); switch (dayString) DateTime lastWeekStartDate = dtTempDate; dtTempEndDate = dtTempDate.Date.AddDays(-1); if (DateTime.Compare(dtTempEndDate, dtTempStartDate) > 0) C#根据时间范围获取每年每月每周的分组 标签:一周 rem count art 一点 tostring datetime star article 原文地址:https://www.cnblogs.com/liuslayer/p/12495684.html
/// 根据时间范围获取每年每月每周的分组
///
/// 起始时间
/// 结束时间
///
///
public static Dictionary
{
Dictionary
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 {
//某年某月一共有多少天
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;
}
-------------------------------------------------------------------------------------------------------------------------
///
/// 根据时间范围(年)获取每月每周的分组
///
/// 每周起始结束键值对
/// 起始时间
/// 结束时间
///
public static void GetGroupWeekByYear(Dictionary
{
//不同月
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 {
//某年某月一共有多少天
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);
}
}
-------------------------------------------------------------------------------------------------------------------------
///
/// 根据时间范围(月)获取每月每周的分组
///
/// 每周起始结束键值对
/// 起始时间
/// 结束时间
///
public static void GetGroupWeekByMonth(Dictionary
{
//一周
if ((dtEndDate.Day - dtStartDate.Day) {
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) {
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 {
dtTempDate = dtTempStartDate.Date.AddDays(+6);
dict.Add(dtTempStartDate.ToString(), dtTempDate.ToString());
dtTempStartDate = dtTempDate.Date.AddDays(+1); ;
}
}
}
}
————————————————
版权声明:本文为CSDN博主「GlenPeng」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/pengzhen8805/article/details/42460797
{
Dictionary
DayOfWeek day = dtStartDate.DayOfWeek;
string dayString = day.ToString();
DateTime dtTempEndDate = dtEndDate;
DateTime dtTempDate = DateTime.Now;
{
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(dtStartDate, dtEndDate);
return dict;
}
dict.Add(dtTempStartDate, dtTempDate);
day = dtEndDate.DayOfWeek;
dayString = day.ToString();
{
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;
}
{
TimeSpan ts1 = new TimeSpan(dtTempStartDate.Ticks);
TimeSpan ts2 = new TimeSpan(dtTempEndDate.Ticks);
TimeSpan ts3 = ts2.Subtract(ts1); //ts2-ts1
long totalDays = (long)ts3.TotalDays + 1;
long WhileCount = (totalDays / 7);
if (WhileCount == 0)
{
dict.Add(dtTempStartDate, dtTempEndDate);
}
else
{
for (int i = 0; i {
dtTempDate = dtTempStartDate.Date.AddDays(+6);
dict.Add(dtTempStartDate, dtTempDate);
dtTempStartDate = dtTempDate.Date.AddDays(+1); ;
}
}
}
dict.Add(lastWeekStartDate, dtEndDate);
return dict;
}