根据用户输入的年份和季度。获取该季度的开始和结束时间
/// <summary> /// 根据年份和季度。获取该季度的开始和结束时间 /// </summary> /// <param name="quarter">季度</param> /// <returns></returns> private void GetQurater(int year,string quarter) { DateTime dt = new DateTime(); switch (quarter) { case "第一季度": dt = new DateTime(year, 1, 1); break; case "第二季度": dt = new DateTime(year, 4, 1); break; case "第三季度": dt = new DateTime(year, 7, 1); break; case "第四季度": dt = new DateTime(year, 10, 1); break; } //本季度初 string start = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day).ToString("yyyy-MM-01"); //获取本季度的最后一天 string end = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day).AddMonths(3).AddDays(-1).ToString("yyyy-MM-dd"); }
第二种
/// <summary> /// 根据季度 获取开始时间和结束时间 /// </summary> /// <param name="quarter">季度</param> /// <returns></returns> private Tuple<DateTime, DateTime> GetQurater(string quarter) { DateTime dt = new DateTime(); switch (quarter) { case "一季度": dt = DateTime.Parse($"{DateTime.Now.Year},1, 01"); break; case "二季度": dt = DateTime.Parse($"{DateTime.Now.Year},4, 01"); break; case "三季度": dt = DateTime.Parse($"{DateTime.Now.Year},7, 01"); break; case "四季度": dt = DateTime.Parse($"{DateTime.Now.Year},10, 01"); break; } //本季度初 string start = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day).ToString("yyyy-MM-01 00:00:00"); //本季度初 //获取本季度的最后一天 string end = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day).AddMonths(3).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"); ; // return Tuple.Create(DateTime.Parse(start), DateTime.Parse(end)); }