//C# Get the time periods such as this week, next week, this month, next month, and this quarter based on the current time
DateTime dt = ; //Current time
DateTime startWeek = (1 - Convert.ToInt32(("d"))); //This week's Monday
DateTime endWeek = (6); //Sun this week
DateTime startMonth = (1 - ); //Early this month
DateTime endMonth = (1).AddDays(-1); //At the end of this month
DateTime startQuarter = (0 - ( - 1) % 3).AddDays(1 - ); //Early this quarter
DateTime endQuarter = (3).AddDays(-1); //At the end of this quarter
DateTime startYear = new DateTime(, 1, 1); //Early this year
DateTime endYear = new DateTime(, 12, 31); //At the end of this year
//As for yesterday, tomorrow, last week, last month, last quarter, last year, etc., just combine the methods of AddDays(), AddMonths(), and AddYears().
//If you don't understand, you should understand it by looking at the Chinese method of displaying the day of the week
// Since DayOfWeek returns a number of days of the week, we need to convert it into Chinese characters for us to read. Some people may use switch to compare one by one, but in fact, it doesn't have to be so troublesome.
string[] Day = new string[] { "Sunday", "Monday", "Tuesday", "Thursday", "Friday", "Saturday" };
string week = Day[Convert.ToInt32(("d"))].ToString();
//Last week, similarly, one week was 7 days, last week was minus 7 days this week, and the same is true for next week.
(Convert.ToInt32 (1 - Convert.ToInt32()) - 7); //Last Monday
(Convert.ToInt32 (1 - Convert.ToInt32()) - 7).AddDays(6); //Last weekend (Sunday)
//next week
(Convert.ToInt32 (1 - Convert.ToInt32()) + 7); //Next Monday
(Convert.ToInt32(1 - Convert.ToInt32()) + 7).AddDays(6); //Next weekend
//This month, many people will say that the first day of this month must be the first day, and the last day is to reduce the next day on the first day. Of course this is correct
//General writing
() + () + "1"; //The first day
(() + () +"1").AddMonths(1).AddDays(-1).ToShortDateString();//Last day
//Skillfully formatting the ToString characters in C# is easier
("yyyy-MM-01");
(("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();
//Last month, minus one month
(("yyyy-MM-01")).AddMonths(-1).ToShortDateString();
(("yyyy-MM-01")).AddDays(-1).ToShortDateString();
//Next month, add one month
(("yyyy-MM-01")).AddMonths(1).ToShortDateString();
(("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToShortDateString();
//7 days later
();
(7).ToShortDateString();
//7 days ago
(-7).ToShortDateString();
();
//For this year, we can easily calculate the first and last days of the year with ToString's characters formatting.
(("yyyy-01-01")).ToShortDateString();
(("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString();
//No need to explain last year
(("yyyy-01-01")).AddYears(-1).ToShortDateString();
(("yyyy-01-01")).AddDays(-1).ToShortDateString();
//Next year
(("yyyy-01-01")).AddYears(1).ToShortDateString();
(("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString();
//This quarter, many people will find it difficult here and need to write a long process to judge. Actually, it doesn't work. We all know that four quarters a year, three months a quarter
//First of all, we push the date to the first month of this quarter, and then the first day of this month is the first day of this quarter
(0 - (( - 1) % 3)).AddDays(1 - );
//Similarly, the last day of this quarter is the first day of next quarter to reduce one
((3 - (( - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
//I believe you all know about it next quarter. . . . knock off
(3 - (( - 1) % 3)).ToString("yyyy-MM-01");
((6 - (( - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
//Last quarter
(-3 - (( - 1) % 3)). AddDays(1 - );
(0 - (( - 1) % 3)).AddDays(1 - ).AddDays(-1).ToShortDateString();