When you make a report or query, you will have to preset some optional date range for the user (pictured above)
Such as this year's sales, this quarter profit, new customers this month
The built-in datetime in C # can basically implement these functions, and using DateTime will make it easier for you to deal with these things.
Today
DateTime.Now.Date.ToShortDateString ();
Yesterday, is today's date minus one
DateTime.Now.AddDays (-1). ToShortDateString ();
Tomorrow, by the same token, add a
DateTime.Now.AddDays (1). ToShortDateString ();
This week, knowing that the first day of the week is the day of the week, knowing that the first day of the week is a few days ago, note that every week is from Sunday to Saturday.
DateTime.Now.AddDays (Convert.todouble (0-convert.toint16 (DateTime.Now.DayOfWeek))). ToShortDateString ();
DateTime.Now.AddDays (Convert.todouble (6-convert.toint16 (DateTime.Now.DayOfWeek))). ToShortDateString ();
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"};
Day[convert.toint16 (DateTime.Now.DayOfWeek)];
Last week, the same thing, a week is 7 days, last week is this week minus 7 days, and next week is the same
DateTime.Now.AddDays (Convert.todouble (0-convert.toint16 (DateTime.Now.DayOfWeek))-7). ToShortDateString ();
DateTime.Now.AddDays (Convert.todouble (6-convert.toint16 (DateTime.Now.DayOfWeek))-7). ToShortDateString ();
Next week
DateTime.Now.AddDays (Convert.todouble (0-convert.toint16 (DateTime.Now.DayOfWeek)) + 7). ToShortDateString ();
DateTime.Now.AddDays (Convert.todouble (6-convert.toint16 (DateTime.Now.DayOfWeek)) + 7). ToShortDateString ();
This month, many people will say that the first day of this month is definitely number 1th, the last day is next month, one more day. Of course, it's right.
The general wording
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's easy to figure out the first and last days of the year with the characters of ToString.
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). ToShortDateString ();
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). Addyears (1). AddDays (-1). ToShortDateString ();
Last year, there's no need to explain.
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 feel the difficulty here, need to write a long process to judge. In fact, we all know that four quarters a year, a quarter of three months
First we push the date to the first month of the quarter, and the first day of the month is the first day of the quarter.
DateTime.Now.AddMonths (0-((datetime.now.month-1)% 3)). ToString ("yyyy-mm-01");
Similarly, the last day of the quarter is the first day of the next quarter minus a
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 .... Wrap
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)). ToString ("yyyy-mm-01");
DateTime.Parse (DateTime.Now.AddMonths (0-(datetime.now.month-1)% 3)). ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString ();