参考:https://www.iteye.com/blog/xqf222-1920357
https://blog.csdn.net/u011981242/article/details/51595546
/// <summary> /// 获取时间段(某段时间的0时0分0秒-23时59分59秒) /// </summary> public class DateTimeHelper { /// <summary> /// 当前时间 /// </summary> //public static DateTime dt = DateTime.Now; //当前时间 //DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") //24小时制 //DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") //12小时制 /// <summary> /// 今天开始,0时0分0秒: /// </summary> public static DateTime startCurrentDay= Convert.ToDateTime(DateTime.Now.ToString("D").ToString()); /// <summary> /// 今天结束,23时59分59秒: /// </summary> //public static DateTime endCurrentDay = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("D").ToString()).AddSeconds(-1); //public static DateTime endCurrentDay = DateTime.Now.Date.AddDays(1).AddSeconds(-1); public static DateTime endCurrentDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59); /// <summary> /// 昨天开始 /// </summary> public static DateTime startLastDay = DateTime.Now.AddDays(-1).Date; /// <summary> /// 昨天结束 /// </summary> public static DateTime endLastDay = startLastDay.AddDays(1).AddSeconds(-1); /// <summary> /// 今天星期几 /// </summary> public static int currentDayOfWeek = Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d")); /// <summary> /// 本周周一 /// </summary> public static DateTime startCurrentWeek = DateTime.Now.AddDays(1 - ((currentDayOfWeek == 0) ? 7 : currentDayOfWeek)).Date; /// <summary> /// 本周周日 /// </summary> public static DateTime endCurrentWeek = startCurrentWeek.AddDays(7).AddSeconds(-1); /// <summary> /// 上周周一 /// </summary> public static DateTime startLastWeek = startCurrentWeek.AddDays(-7).Date; /// <summary> /// 上周周日 /// </summary> public static DateTime endLastWeek = startLastWeek.AddDays(7).AddSeconds(-1); /// <summary> /// 本月月初 /// </summary> public static DateTime startCurrentMonth = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date; //本月月初 /// <summary> /// 本月月末 /// </summary> public static DateTime endCurrentMonth = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1); //本月月末 //DateTime endMonth = startMonth.AddDays((dt.AddMonths(1) - dt).Days - 1); //本月月末 /// <summary> /// 上月月初 /// </summary> public static DateTime startLastMonth = DateTime.Now.AddMonths(-1).Date.AddDays(1 - DateTime.Now.Day); /// <summary> /// 上月月末 /// </summary> public static DateTime endLastMonth = DateTime.Parse(DateTime.Now.AddDays(1 - DateTime.Now.Day).ToShortDateString()).AddSeconds(-1); /// <summary> /// 本季度初 /// </summary> public static DateTime startCurrentQuarter = DateTime.Now.AddMonths(0 - (DateTime.Now.Month - 1) % 3).AddDays(1 - DateTime.Now.Day).Date; //本季度初 /// <summary> /// 本季度末 /// </summary> public static DateTime endCurrentQuarter = startCurrentQuarter.AddMonths(3).AddSeconds(-1); //本季度末 /// <summary> /// 上季度初 /// </summary> public static DateTime startLastQuarter = startCurrentQuarter.AddMonths(-3).Date; /// <summary> /// 上季度末 /// </summary> public static DateTime endLastQuarter = startLastQuarter.AddMonths(3).AddSeconds(-1); /// <summary> /// 今年年初 /// </summary> public static DateTime startCurrentYear = new DateTime(DateTime.Now.Year, 1, 1).Date; //本年年初 /// <summary> /// 今年年末 /// </summary> public static DateTime endCurrentYear = new DateTime(DateTime.Now.Year, 12,31, 23, 59, 59); //本年年末 /// <summary> /// 去年年初 /// </summary> public static DateTime startLastYear = startCurrentYear.AddYears(-1).Date; /// <summary> /// 去年年末 /// </summary> public static DateTime endLastYear = endCurrentYear.AddYears(-1); //至于昨天、明天、上周、上月、上季度、上年度等等,只要AddDays()、AddMonths()、AddYears()这几种方法组合一下就可以了。 }