Conversion of common date operations

Source: Internet
Author: User

1. // Today
2. DateTime. Now. Date. tow.datestring ();
3. // yesterday, that is, one minus today's date
4. DateTime. Now. AddDays (-1). tow.datestring ();
5. // tomorrow. Similarly, add one
6. DateTime. Now. AddDays (1). tow.datestring ();
7.
8. // 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.
9. DateTime. Now. AddDays (Convert. ToDouble (0-Convert. ToInt16 (DateTime. Now. DayOfWeek). ToShortDateString ();
10. DateTime. Now. AddDays (Convert. ToDouble (6-Convert. ToInt16 (DateTime. Now. DayOfWeek). ToShortDateString ();
11. // if you still don't understand it, you should understand the Chinese display method of the day of the week.
12. // because DayOfWeek returns the number of the day of the week, we need to convert it into Chinese characters so that we can read it easily. Some people may use the switch to compare it one by one. In fact, this is not so troublesome.
13. string [] Day = new string [] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday "};
14. Day [Convert. ToInt16 (DateTime. Now. DayOfWeek)];
15.
16. // last week. Similarly, one week is 7 days, and the last week is the week minus 7 days. The same is true for the next week.
17. DateTime. Now. AddDays (Convert. ToDouble (0-Convert. ToInt16 (DateTime. Now. DayOfWeek)-7). tow.datestring ();
18. DateTime. Now. AddDays (Convert. ToDouble (6-Convert. ToInt16 (DateTime. Now. DayOfWeek)-7). tow.datestring ();
19. // next week
20. DateTime. Now. AddDays (Convert. ToDouble (0-Convert. ToInt16 (DateTime. Now. DayOfWeek) + 7). tow.datestring ();
21. DateTime. Now. AddDays (Convert. ToDouble (6-Convert. ToInt16 (DateTime. Now. DayOfWeek) + 7). tow.datestring ();
22. // 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
23. // General statement
24. DateTime. Now. Year. ToString () + DateTime. Now. Month. ToString () + "1"; // The first day
25. dateTime. parse (DateTime. now. year. toString () + DateTime. now. month. toString () + "1 "). addMonths (1 ). addDays (-1 ). toShortDateString (); // last day
26.
27. // use the ToString character in C # To format it more easily
28. DateTime. Now. ToString ("yyyy-MM-01 ");
29. DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (1). AddDays (-1). to1_datestring ();
30.
31. // last month, minus a month
32. DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (-1). to1_datestring ();
33. DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddDays (-1). to1_datestring ();
34. // Add a month to the next month
35. DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (1). to1_datestring ();
36. DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (2). AddDays (-1). to1_datestring ();
37. // 7 days later
38. DateTime. Now. Date. tow.datestring ();
39. DateTime. Now. AddDays (7). tow.datestring ();
40. // 7 days ago
41. DateTime. Now. AddDays (-7). tow.datestring ();
42. DateTime. Now. Date. tow.datestring ();
43.
44. // for this year, we can easily format it with the ToString character and calculate the first and last days of this year.
45. DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). to1_datestring ();
46. DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (1). AddDays (-1). to1_datestring ();
47. // the previous year. Don't explain it again.
48. DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (-1). to1_datestring ();
49. DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddDays (-1). to1_datestring ();
50. // next year
51. DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (1). to1_datestring ();
52. DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (2). AddDays (-1). to1_datestring ();
53.
54. // 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.
55. // first, we will push the date to the first month of this quarter, and then the first day of this month will be the first day of this quarter.
56. DateTime. Now. AddMonths (0-(DateTime. Now. Month-1) % 3). ToString ("yyyy-MM-01 ");
57. // Similarly, the last day of this quarter is the first day of the next quarter minus one
58. dateTime. parse (DateTime. now. addMonths (3-(DateTime. now. month-1) % 3 )). toString ("yyyy-MM-01 ")). addDays (-1 ). toShortDateString ();
59. // next quarter, I believe you all know .... Close
60. DateTime. Now. AddMonths (3-(DateTime. Now. Month-1) % 3). ToString ("yyyy-MM-01 ");
61. dateTime. parse (DateTime. now. addMonths (6-(DateTime. now. month-1) % 3 )). toString ("yyyy-MM-01 ")). addDays (-1 ). toShortDateString ();
62. // last quarter
63. DateTime. Now. AddMonths (-3-(DateTime. Now. Month-1) % 3). ToString ("yyyy-MM-01 ");
64. 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.