The built-in DateTime in C # can basically implement these functions.

Source: Internet
Author: User
    1. // Some optional date ranges (for example) are preset for users when making reports or queries)
    2. // Such as annual sales, quarterly profit, and new customers this month
    3. // 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.
    4. // Today
    5. Datetime. Now. Date. tow.datestring ();
    6. // Yesterday, that is, one minus today's date
    7. Datetime. Now. adddays (-1). tow.datestring ();
    8. // Tomorrow, similarly, add one
    9. Datetime. Now. adddays (1). tow.datestring ();
    10. // 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.
    11. Datetime. Now. adddays (convert. todouble (0-convert. toint16 (datetime. Now. dayofweek). toshortdatestring ();
    12. Datetime. Now. adddays (convert. todouble (6-convert. toint16 (datetime. Now. dayofweek). toshortdatestring ();
    13. // If you still don't understand it, you should understand how to display the day of the week in Chinese.
    14. // 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.
    15. String [] Day = new string [] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday "};
    16. String DayName = Day [Convert. ToInt16 (DateTime. Now. DayOfWeek)];
    17. // Last week. Similarly, a week is 7 days. Last week is the week minus 7 days. The same is true for next week.
    18. DateTime. Now. AddDays (Convert. ToDouble (0-Convert. ToInt16 (DateTime. Now. DayOfWeek)-7). ToShortDateString ();
    19. DateTime. Now. AddDays (Convert. ToDouble (6-Convert. ToInt16 (DateTime. Now. DayOfWeek)-7). ToShortDateString ();
    20. // Next week
    21. DateTime. Now. AddDays (Convert. ToDouble (0-Convert. ToInt16 (DateTime. Now. DayOfWeek) + 7). ToShortDateString ();
    22. DateTime. Now. AddDays (Convert. ToDouble (6-Convert. ToInt16 (DateTime. Now. DayOfWeek) + 7). ToShortDateString ();
    23. // 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
    24. // Easy to format with the ToString character in C #
    25. DateTime. Now. ToString ("yyyy-MM-01 ");
    26. DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (1). AddDays (-1). to1_datestring ();
    27. // Last month, minus a month
    28. DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (-1). to1_datestring ();
    29. DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddDays (-1). to1_datestring ();
    30. // Add a month to the next month
    31. DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (1). to1_datestring ();
    32. DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (2). AddDays (-1). to1_datestring ();
    33. // 7 days later
    34. DateTime. Now. Date. tow.datestring ();
    35. DateTime. Now. AddDays (7). tow.datestring ();
    36. // Seven days ago
    37. DateTime. Now. AddDays (-7). tow.datestring ();
    38. DateTime. Now. Date. tow.datestring ();
    39. // During the current year, it is easy for us to format with ToString characters to calculate the first and last days of the year.
    40. DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). to1_datestring ();
    41. DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (1). AddDays (-1). to1_datestring ();
    42. // For the previous year, do not explain it again
    43. DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (-1). to1_datestring ();
    44. DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddDays (-1). to1_datestring ();
    45. // Next year
    46. DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (1). to1_datestring ();
    47. DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (2). AddDays (-1). to1_datestring ();
    48. // 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.
    49. // 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.
    50. DateTime. Now. AddMonths (0-(DateTime. Now. Month-1) % 3). ToString ("yyyy-MM-01 ");
    51. // Similarly, the last day of this quarter is the first day of the next quarter minus one
    52. Datetime. parse (datetime. now. addmonths (3-(datetime. now. month-1) % 3 )). tostring ("yyyy-MM-01 ")). adddays (-1 ). toshortdatestring ();
    53. // Next quarter, I believe you all know .... Close
    54. Datetime. Now. addmonths (3-(datetime. Now. Month-1) % 3). tostring ("yyyy-MM-01 ");
    55. Datetime. parse (datetime. now. addmonths (6-(datetime. now. month-1) % 3 )). tostring ("yyyy-MM-01 ")). adddays (-1 ). toshortdatestring ();
    56. // Last quarter
    57. Datetime. Now. addmonths (-3-(datetime. Now. Month-1) % 3). tostring ("yyyy-MM-01 ");
    58. Datetime. parse (datetime. now. addmonths (0-(datetime. now. month-1) % 3 )). tostring ("yyyy-MM-01 ")). adddays (-1 ). toshortdatestring ();

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.