C#常用获取本周、本月、本季度、本年的时间起止段代码
/// <summary> /// 今日 /// </summary> /// <param name="start"></param> /// <param name="end"></param> public static void GetRangeToday(out DateTime start, out DateTime end) { start = DateTime.Now.Date; end = start; } /// <summary> /// 本周 /// </summary> /// <param name="start"></param> /// <param name="end"></param> public static void GetRangeThisWeek(out DateTime start, out DateTime end) { DateTime dt = DateTime.Now.Date; start = dt.AddDays(-(int)dt.DayOfWeek); end = start.AddDays(6); } /// <summary> /// 本月 /// </summary> /// <param name="start"></param> /// <param name="end"></param> public static void GetRangeThisMonth(out DateTime start, out DateTime end) { DateTime dt = DateTime.Now; start = new DateTime(dt.Year, dt.Month, 1); end = start.AddMonths(1).AddDays(-1); } /// <summary> /// 本季度 /// </summary> /// <param name="start"></param> /// <param name="end"></param> public static void GetRangeThisSeason(out DateTime start, out DateTime end) { DateTime dt = DateTime.Now; int month = (dt.Month - 1)/3 * 3 + 1; start = new DateTime(dt.Year, month, 1); end = start.AddMonths(3).AddDays(-1); } /// <summary> /// 本季度 /// </summary> /// <param name="start"></param> /// <param name="end"></param> public static void GetRangeThisYear(out DateTime start, out DateTime end) { DateTime dt = DateTime.Now; start = new DateTime(dt.Year, 1, 1); end = start.AddYears(1).AddDays(-1); }