C# 获取两个时间段之间的所有时间与获取当前时间所在的季度开始和结束时间
一:C# 获取两个时间段之间的所有时间
public List<string> GetTimeList(string rq1, string rq2)
{
List<string> timeList = new List<string>();
//首先保证 rq1<=rq2
DateTime time1 = Convert.ToDateTime(rq1);
DateTime time2 = Convert.ToDateTime(rq2);
while (time1 <= time2)
{
timeList.Add(time1.ToString("yyyy-MM-dd"));
time1 = time1.AddDays(1);
}
return timeList;
}
使用:
List<string> sjList = GetTimeList(sj1, sj2);
二:获取当前时间所在季度的开始和结束时间
//本季度
private List<string> FunSelectSJNow(string jd)
{
List<string> lis = new List<string>();
string NowYear = jd.Substring(0, 4); //截取当前年份
string Nowjd = jd.Substring(jd.Length - 2, 2); //截取当前季度
string StartTime = ""; //当前季度的开始时间
string endTime = ""; //当前季度的结束时间
if (Nowjd == "01")
{
StartTime = "-01-01";
endTime = "-03-01";
}
if (Nowjd == "02")
{
StartTime = "-04-01";
endTime = "-06-01";
}
if (Nowjd == "03")
{
StartTime = "-07-01";
endTime = "-09-01";
}
if (Nowjd == "04")
{
StartTime = "-10-01";
endTime = "-12-01";
}
StartTime = NowYear + StartTime;
endTime = NowYear + endTime;
lis.Add(StartTime);
lis.Add(endTime);
return lis;
}
//上季度
private List<string> FunSelectLastSJ(string jd)
{
List<string> lis = new List<string>();
string NowYear = jd.Substring(0, 4); //截取当前年份
string Nowjd = jd.Substring(jd.Length - 2, 2); //截取当前季度
string StartTime = ""; //上季度的开始时间
string endTime = ""; //上季度的结束时间
string endYear = ""; //上季度年
string endjd = ""; //上季度季度
if (Nowjd == "01")
{
endYear = (Convert.ToInt32(NowYear) -1).ToString() ;
endjd = "Q4";
}
if (Nowjd == "02")
{
endYear = NowYear;
endjd = "Q1";
}
if (Nowjd == "03")
{
endYear = NowYear;
endjd = "Q2";
}
if (Nowjd == "04")
{
endYear = NowYear;
endjd = "Q3";
}
if (endjd == "Q1")
{
StartTime = "-01-01";
endTime = "-03-01";
}
if (endjd == "Q2")
{
StartTime = "-04-01";
endTime = "-06-01";
}
if (endjd == "Q3")
{
StartTime = "-07-01";
endTime = "-09-01";
}
if (endjd == "Q4")
{
StartTime = "-10-01";
endTime = "-12-01";
}
StartTime = endYear + StartTime;
endTime = endYear + endTime;
lis.Add(StartTime);
lis.Add(endTime);
return lis;
}
使用:
List<string> li = FunSelectSJNow(NowTime);
rq1 = li[0];
rq2 = li[1];
// jd: Q1,Q2,Q3,Q4 (四个季度)
List<string> li = FunSelectLastSJ(jd);
rq1 = li[0];
rq2 = li[1];