C # obtain the start and end times of the week, month, quarter, and year based on the current time,

Source: Internet
Author: User

(Conversion) C # obtain the start and end times of the week, month, quarter, and year based on the current time,

DateTime dt = DateTime. now; // The current time DateTime startWeek = dt. addDays (1-Convert. toInt32 (dt. dayOfWeek. toString ("d"); // DateTime endWeek = startWeek on Monday of this week. addDays (6); // DateTime startMonth = dt on Sunday of this week. addDays (1-dt. day); // DateTime endMonth = startMonth at the beginning of this month. addMonths (1 ). addDays (-1); // The End of the month // endMonth = startMonth. addDays (dt. addMonths (1)-dt ). days-1); // DateTime startQuarter = dt at the end of the month. addMonths (0-(dt. month-1) % 3 ). addDays (1-dt. day); // DateTime endQuarter = startQuarter at the beginning of this quarter. addMonths (3 ). addDays (-1); // DateTime startYear = new DateTime (dt. year, 1, 1); // DateTime endYear = new DateTime (dt. year, 12, 31); // at the end of this Year, for example, yesterday, tomorrow, last week, last month, last quarter, and last Year, var last Monday = DateTime. now. addDays (Convert. toInt32 (1-Convert. toInt32 (DateTime. now. dayOfWeek)-7); // last Monday var last weekend = DateTime. now. addDays (Convert. toInt32 (1-Convert. toInt32 (DateTime. now. dayOfWeek)-7 ). addDays (6); // last weekend (Sunday) // next week var = DateTime. now. addDays (Convert. toInt32 (1-Convert. toInt32 (DateTime. now. dayOfWeek) + 7); // next Monday var next weekend = DateTime. now. addDays (Convert. toInt32 (1-Convert. toInt32 (DateTime. now. dayOfWeek) + 7 ). addDays (6); // DateTime next weekend. parse (DateTime. now. year. toString () + DateTime. now. month. toString () + "1 "). addMonths (1 ). addDays (-1 ). toShortDateString (); // The Last Day // use the ToString character in C # To Format DateTime more easily. now. toString ("yyyy-MM-01"); // DateTime at the beginning of this month. parse (DateTime. now. toString ("yyyy-MM-01 ")). addMonths (1 ). addDays (-1 ). toShortDateString (); // DateTime on the last day of the month. parse (DateTime. now. toString ("yyyy-MM-01 ")). addMonths (-1 ). toShortDateString (); // 1st day of last month DateTime. parse (DateTime. now. toString ("yyyy-MM-01 ")). addDays (-1 ). toShortDateString (); // DateTime on the last day of the last month. parse (DateTime. now. toString ("yyyy-MM-01 ")). addMonths (1 ). toShortDateString (); // 1st day of next month DateTime. parse (DateTime. now. toString ("yyyy-MM-01 ")). addMonths (2 ). addDays (-1 ). toShortDateString (); // DateTime of the last day of next month. now. addDays (7 ). toShortDateString (); // DateTime after 7 days. now. addDays (-7 ). toShortDateString (); // DateTime seven days ago. now. date. tow.datestring (); // this year, formatted with the ToString character, we can easily calculate the first day of the year and DateTime of the last day of the year. parse (DateTime. now. toString ("yyyy-01-01 ")). toShortDateString (); // DateTime on the first day of the current year. parse (DateTime. now. toString ("yyyy-01-01 ")). addYears (1 ). addDays (-1 ). toShortDateString (); // DateTime on the last day of the year. parse (DateTime. now. toString ("yyyy-01-01 ")). addYears (-1 ). toShortDateString (); // the first day of the previous year, DateTime. parse (DateTime. now. toString ("yyyy-01-01 ")). addDays (-1 ). toShortDateString (); // the last day of the previous year, DateTime. parse (DateTime. now. toString ("yyyy-01-01 ")). addYears (1 ). toShortDateString (); // DateTime on the first day of the next year. parse (DateTime. now. toString ("yyyy-01-01 ")). addYears (2 ). addDays (-1 ). toShortDateString (); // the last day of the next year // this quarter, DateTime. now. addMonths (0-(DateTime. now. month-1) % 3 )). addDays (1-DateTime. now. day); // the first Day of the current quarter; DateTime. parse (DateTime. now. addMonths (3-(DateTime. now. month-1) % 3 )). toString ("yyyy-MM-01 ")). addDays (-1 ). toShortDateString (); // DateTime on the last day of the quarter. now. addMonths (3-(DateTime. now. month-1) % 3 )). toString ("yyyy-MM-01"); // the first day of the next quarter DateTime. parse (DateTime. now. addMonths (6-(DateTime. now. month-1) % 3 )). toString ("yyyy-MM-01 ")). addDays (-1 ). toShortDateString (); // DateTime of the last day of the next quarter. now. addMonths (-3-(DateTime. now. month-1) % 3 )). addDays (1-DateTime. now. day); // DateTime on the first Day of the last quarter. now. addMonths (0-(DateTime. now. month-1) % 3 )). addDays (1-DateTime. now. day ). addDays (-1 ). toShortDateString (); // the last day of the last quarter

 

Http://www.cnblogs.com/qinersky902/p/5065870.html (Tualatin)

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.