C # obtain the relevant date, such as a period of time when the report is made (such as this year, this quarter, this month, this week, last week, etc)

Source: Internet
Author: User

[Csharp]
// Some optional date ranges (for example) are preset for users when making reports or queries)
// Such as annual sales, quarterly profit, and new customers this month
// The built-in DateTime in C # can basically implement these functions. The clever use of DateTime will make it much easier for you to handle these tasks.
 
// Today
DateTime. Now. Date. tow.datestring ();
// Yesterday, that is, one minus today's date
DateTime. Now. AddDays (-1). tow.datestring ();
// Tomorrow, similarly, add one
DateTime. Now. AddDays (1). tow.datestring ();
 
// This Week (to know the first day of this week, you must first know the day of the week, so that the first day of this week is the day of the day a few days ago, note that every week starts 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 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 DayName = Day [Convert. ToInt16 (DateTime. Now. DayOfWeek)];
// 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. 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 must be the first day of the month, and the last day is the second day of the month. Of course this is correct
// 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 every four quarters of a year, one quarter, three months www.2cto.com
// 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). ToString ("yyyy-MM-01 ");
// 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). ToString ("yyyy-MM-01 ");
DateTime. parse (DateTime. now. addMonths (0-(DateTime. now. month-1) % 3 )). toString ("yyyy-MM-01 ")). addDays (-1 ). toShortDateString ();
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.