C # Gets the current date, time, quarter

Source: Internet
Author: User

1DateTime dt = DateTime.Now;//Current Time2DateTime startweek = dt. AddDays (1-Convert.ToInt32 (dt. Dayofweek.tostring ("D")));//this Monday3DateTime Endweek = Startweek.adddays (6);//this Sunday4 5DateTime startmonth = dt. AddDays (1-DT. Day);//Early this month6DateTime Endmonth = startmonth.addmonths (1). AddDays (-1);//Month End//7 8Endmonth = startmonth.adddays (dt. AddMonths (1)-DT). Days-1);//Month End9DateTime startquarter = dt. AddMonths (0-(dt. Month-1) %3). AddDays (1-DT. Day);//At the beginning of the quarterTenDateTime Endquarter = startquarter.addmonths (3). AddDays (-1);//At the end of the quarter One  ADateTime startyear =NewDateTime (dt. Year,1,1);//early this year -DateTime endyear =NewDateTime (dt. Year, A, to);//at the end of this year for yesterday, tomorrow, last week, last month, last quarter, last year, etc. -  the varLast Monday = DateTime.Now.AddDays (Convert.ToInt32 (1-Convert.ToInt32 (DateTime.Now.DayOfWeek))-7);//last Monday - varLast weekend = DateTime.Now.AddDays (Convert.ToInt32 (1-Convert.ToInt32 (DateTime.Now.DayOfWeek))-7). AddDays (6);//last weekend (Sunday)//next week - varNext Monday = DateTime.Now.AddDays (Convert.ToInt32 (1-Convert.ToInt32 (DateTime.Now.DayOfWeek)) +7);//Next Monday - varNext weekend = DateTime.Now.AddDays (Convert.ToInt32 (1-Convert.ToInt32 (DateTime.Now.DayOfWeek)) +7). AddDays (6);//next weekend +  -DateTime.Parse (DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () +"1"). AddMonths (1). AddDays (-1). ToShortDateString ();//Last day + //It 's easier to use ToString's character formatting in C # ADateTime.Now.ToString ("yyyy-mm-01");//early this month atDateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddMonths (1). AddDays (-1). ToShortDateString ();//last day of the month -DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddMonths (-1). ToShortDateString ();//last month, 1th . -DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString ();//last day of last month -DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddMonths (1). ToShortDateString ();//next month, number 1th . -DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddMonths (2). AddDays (-1). ToShortDateString ();//The last day of next month -DateTime.Now.AddDays (7). ToShortDateString ();//after 7 days inDateTime.Now.AddDays (-7). ToShortDateString ();//7 days ago -DateTime.Now.Date.ToShortDateString ();//This year, formatting with ToString's characters makes it easy to figure out the first and last day of the year to  +DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). ToShortDateString ();//first day of the year -DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). AddYears (1). AddDays (-1). ToShortDateString ();//last day of the year the  *DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). AddYears (-1). ToShortDateString ();//on the first day of last year, $DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). AddDays (-1). ToShortDateString ();//on the last day of the previous year,Panax Notoginseng  -DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). AddYears (1). ToShortDateString ();//first day of next year theDateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). AddYears (2). AddDays (-1). ToShortDateString ();//last day of next year + //this quarter, ADateTime.Now.AddMonths (0-((DateTime.Now.Month-1) %3)). AddDays (1-DateTime.Now.Day);//the first day of the quarter; theDateTime.Parse (DateTime.Now.AddMonths (3-((DateTime.Now.Month-1) %3)). ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString ();//The last day of the quarter +DateTime.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 ();//last day of next quarter $  $DateTime.Now.AddMonths (-3-((DateTime.Now.Month-1) %3)). AddDays (1-DateTime.Now.Day);//first day of last quarter -DateTime.Now.AddMonths (0-((DateTime.Now.Month-1) %3)). AddDays (1-DateTime.Now.Day). AddDays (-1). ToShortDateString ();//last day of the previous quarter

C # Gets the current date, time, quarter

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.