Recently, there is a demand for statistical distribution. It is necessary to count the distribution trend of this week, last week, this month, last month, this quarter, last quarter, this year, and last year, so it involves calculating weeks, months, and quarters. , the start and end times of the year, etc., the following summarizes the method of obtaining the start and end times of the week, month, quarter, year and other time periods according to the current time in C#. Not much nonsense, just paste the code directly, if you find it useful, please feel free recommend.
DateTime dt = DateTime.Now; //current time DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //Monday this week DateTime endWeek = startWeek.AddDays(6); //This week Sunday DateTime startMonth = dt.AddDays(1 - dt.Day); //The beginning of the month DateTime endMonth = startMonth.AddMonths(1).AddDays(-1); //end of this month// endMonth = startMonth.AddDays((dt.AddMonths(1) - dt).Days - 1); //end of this month DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day); //beginning of the quarter DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1); //end of the quarter DateTime startYear = new DateTime(dt.Year, 1, 1); //beginning of the year DateTime endYear = new DateTime(dt.Year, 12, 31); //At the end of this year, as for yesterday, tomorrow, last week, last month, last quarter, last year, etc., var Last Monday = DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) - 7); //Last Monday var last weekend = DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) - 7).AddDays(6); // last weekend (Sunday) // next week var next Monday = DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) + 7); //Next Monday var next weekend = DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) + 7).AddDays(6); // next weekend DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();//最后一天 //It is easier to use the character formatting of ToString in C# DateTime.Now.ToString("yyyy-MM-01");//本月初 DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();//The last day of the month DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();//The 1st of last month DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();//Last day of last month DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).ToShortDateString();// 下个月1号 DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToShortDateString();//The last day of the next month DateTime.Now.AddDays(7).ToShortDateString();//7天后 DateTime.Now.AddDays(-7).ToShortDateString();//7天前 DateTime.Now.Date.ToShortDateString();//This year, we can easily calculate the first and last day of the year by formatting the characters of ToString DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToShortDateString();//The first day of the year DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString();//The last day of the year DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToShortDateString(); //The first day of the previous year, DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToShortDateString();//The last day of the previous year, DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).ToShortDateString(); //The first day of the next year DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString();//The last day of the next year // this quarter, DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day);//The first day of the quarter; DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();/ /last day of the quarter DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01");//The first day of the next quarter DateTime.Parse(DateTime.Now.AddMonths(6 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();/ / last day of next quarter DateTime.Now.AddMonths(-3 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day);// 上季度第一天 DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day).AddDays(-1).ToShortDateString();// 上季度最后一天