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