Asp tutorial. net get time periods such as this week, this month, and this quarter
Use of datetime in C #
// If you still don't understand it, you should understand how to display the day of the week in Chinese.
// Because DayOfWeek returns the day of the week of the number, we need to convert it into Chinese characters to facilitate reading. Some people may use the switch to compare them one by one. In fact, this is not so troublesome.
String [] Day = new string [] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday "};
String week = Day [Convert. ToInt32 (DateTime. Now. DayOfWeek. ToString ("d")]. ToString ();
// Last week. Similarly, a week is 7 days. Last week is the week minus 7 days. The same is true for next week.
DateTime. Now. AddDays (Convert. ToInt32 (1-Convert. ToInt32 (DateTime. Now. DayOfWeek); // last Monday
DateTime. Now. AddDays (Convert. ToInt32 (1-Convert. ToInt32 (DateTime. Now. DayOfWeek)-7). AddDays (6); // last weekend (Sunday)
// Next week
DateTime. Now. AddDays (Convert. ToInt32 (1-Convert. ToInt32 (DateTime. Now. DayOfWeek) + 7); // next Monday
DateTime. Now. AddDays (Convert. ToInt32 (1-Convert. ToInt32 (DateTime. Now. DayOfWeek) + 7). AddDays (6); // next weekend
// This month, many people will say that the first day of this month must be the first day of the month, and the last day is the second day of the month. Of course this is correct
// General statement
DateTime. Now. Year. ToString () + DateTime. Now. Month. ToString () + "1"; // The first day
DateTime. parse (DateTime. now. year. toString () + DateTime. now. month. toString () + "1 "). addMonths (1 ). addDays (-1 ). toShortDateString (); // Last Day
// Easy to format with the ToString character in C #
DateTime. Now. ToString ("yyyy-MM-01 ");
DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (1). AddDays (-1). To1_datestring ();
// Last month, minus a month
DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (-1). To1_datestring ();
DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddDays (-1). To1_datestring ();
// Add a month to the next month
DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (1). To1_datestring ();
DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (2). AddDays (-1). To1_datestring ();
// 7 days later
DateTime. Now. Date. Tow.datestring ();
DateTime. Now. AddDays (7). Tow.datestring ();
// Seven days ago
DateTime. Now. AddDays (-7). Tow.datestring ();
DateTime. Now. Date. Tow.datestring ();
// During the current year, it is easy for us to format with ToString characters to calculate the first and last days of the year.
DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). To1_datestring ();
DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (1). AddDays (-1). To1_datestring ();
// For the previous year, do not explain it again
DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (-1). To1_datestring ();
DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddDays (-1). To1_datestring ();
// Next year
DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (1). To1_datestring ();
DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (2). AddDays (-1). To1_datestring ();
// This quarter, many people will find it difficult and need to write a long process to judge. In fact, we don't need to. We all know that a year, four quarters, one quarter, three months.
// First, we will push the date to the first month of the quarter, and then the first day of the month will be the first day of the quarter.
DateTime. Now. AddMonths (0-(DateTime. Now. Month-1) % 3). AddDays (1-DateTime. Now. Day );
// Similarly, the last day of this quarter is the first day of the next quarter minus one
DateTime. parse (DateTime. now. addMonths (3-(DateTime. now. month-1) % 3 )). toString ("yyyy-MM-01 ")). addDays (-1 ). toShortDateString ();
// Next quarter, I believe you all know .... Close
DateTime. Now. AddMonths (3-(DateTime. Now. Month-1) % 3). ToString ("yyyy-MM-01 ");
DateTime. parse (DateTime. now. addMonths (6-(DateTime. now. month-1) % 3 )). toString ("yyyy-MM-01 ")). addDays (-1 ). toShortDateString ();
// Last quarter
DateTime. Now. AddMonths (-3-(DateTime. Now. Month-1) % 3). AddDays (1-DateTime. Now );
DateTime. now. addMonths (0-(DateTime. now. month-1) % 3 )). addDays (1-DateTime. now. day ). addDays (-1 ). toShortDateString ();
DateTime dt = DateTime. Now; // current time
DateTime startWeek = dt. AddDays (1-Convert. ToInt32 (dt. DayOfWeek. ToString ("d"); // Monday of the week
DateTime endWeek = startWeek. AddDays (6); // Sunday of the week
DateTime startMonth = dt. AddDays (1-dt. Day); // The beginning of this month
DateTime endMonth = startMonth. AddMonths (1). AddDays (-1); // end of the month
// DateTime endMonth = startMonth. AddDays (dt. AddMonths (1)-dt). Days-1); // end of the month
DateTime startQuarter = dt. AddMonths (0-(dt. Month-1) % 3). AddDays (1-dt. Day); // at the beginning of this quarter
DateTime endQuarter = startQuarter. AddMonths (3). AddDays (-1); // end of this quarter
DateTime startYear = new DateTime (dt. Year, 1, 1); // early this Year
DateTime endYear = new DateTime (dt. Year, 12, 31); // end of the Year
For yesterday, tomorrow, last week, last month, last quarter, and last year, you only need to combine AddDays (), AddMonths (), and AddYears.