C # gets this week, next week, month, next month, quarter, and so on according to the current time period
DateTime dt = DateTime.Now; Current time
DateTime startweek = dt. AddDays (dt. 1-convert.toint32. Dayofweek.tostring ("D"))); This week Monday
DateTime Endweek = startweek.adddays (6); This Sunday
DateTime startmonth = dt. AddDays (1-DT. Day); Earlier this month
DateTime endmonth = startmonth.addmonths (1). AddDays (-1); Month End
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); At the end of the quarter
DateTime startyear = new datetime (dt. Year, 1, 1); Early this 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, and so on, as long as adddays (), Addmonths (), Addyears () The combination of the several methods can be.
If you do not understand, and then look at the Chinese to show the day of the week should understand the way
Because DayOfWeek returns the number of days of the week, we have to convert it to Chinese characters to facilitate our reading, some people may use switch to one by one to control, in fact, 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, by the same token, a week is 7 days, last week is minus 7 days this week, and next week is the same
DateTime.Now.AddDays (Convert.ToInt32 1-convert.toint32 ( DateTime.Now.DayOfWeek))-7; //Previous 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, a lot of people will say that the first day of this month is certainly 1th, the last day is next month, one more day. Of course this is true for
//General Writing
DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + "1";//First day
DateTime.Parse ( DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + "1"). Addmonths (1). AddDays (-1). ToShortDateString ()//Last day
It's easier to format the characters in C # with ToString
DateTime.Now.ToString ("yyyy-mm-01");
DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). Addmonths (1). AddDays (-1). ToShortDateString ();
Last month, minus a month
DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). Addmonths (-1). ToShortDateString ();
DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString ();
Next month, add a month.
DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). Addmonths (1). ToShortDateString ();
DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). Addmonths (2). AddDays (-1). ToShortDateString ();
7 days later
DateTime.Now.Date.ToShortDateString ();
DateTime.Now.AddDays (7). ToShortDateString ();
7 days ago
DateTime.Now.AddDays (-7). ToShortDateString ();
DateTime.Now.Date.ToShortDateString ();
//This year, it is easy to figure out the first and last day of the year
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")) with the characters of ToString. ToShortDateString ();
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). Addyears (1). AddDays (-1). ToShortDateString ();
//Last year, no more explanations
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). Addyears (-1). ToShortDateString ();
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). AddDays (-1). ToShortDateString ();
//Next year
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). Addyears (1). ToShortDateString ();
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). Addyears (2). AddDays (-1). ToShortDateString ();
//This quarter, many people will find it difficult, need to write a long process to judge. In fact, we all know that four quarters a year, a quarter three months
//First we push the date to the first month of the quarter, then the first day of the month is 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 the 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'm sure you all know .... Call it off
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 ();