C # gets Date function classes for Monday and Sunday

Source: Internet
Author: User

#regionGet the dates of the week of Monday and Sunday/// <summary>         ///Calculate Week 1st of week/// </summary>         /// <returns></returns>          Public StaticDateTime getmondaydate () {returngetmondaydate (DateTime.Now); }        /// <summary>         ///calculate dates for this week Sunday/// </summary>         /// <returns></returns>          Public StaticDateTime getsundaydate () {returngetsundaydate (DateTime.Now); }        /// <summary>         ///calculates the date from which a date is started (Day of the week)/// </summary>         /// <param name= "Somedate" >any day of the week</param>         /// <returns>returns the day of the week, followed by the exact time, minute, second, and incoming values equal</returns>          Public Staticdatetime getmondaydate (datetime somedate) {inti = Somedate.dayofweek-Dayofweek.monday; if(i = =-1) i =6;//The I value > = 0, because the enumeration reason, Sunday ranked in the front, at this time sunday-monday=-1, must +7=6. TimeSpan ts =NewTimeSpan (I,0,0,0); returnsomedate.subtract (TS); }        /// <summary>         ///calculates the end date of a day (day of worship)/// </summary>         /// <param name= "Somedate" >any day of the week</param>         /// <returns>returns the day of the week, followed by the exact time, minute, second, and incoming values equal</returns>          Public Staticdatetime getsundaydate (datetime somedate) {inti = Somedate.dayofweek-DayOfWeek.Sunday; if(I! =0) i =7I//for enumeration reasons, the Sunday is ranked first, minus the interval of 7. TimeSpan ts =NewTimeSpan (I,0,0,0); returnsomedate.add (TS); }#endregion

Collection of other methods/////////////////////////////////////

DateTime dt = DateTime.Now;//Current TimeDateTime Startweek= dt. AddDays (1-Convert.ToInt32 (dt. Dayofweek.tostring ("D")));//this MondayDateTime Endweek = Startweek.adddays (6);//this SundayDateTime Startmonth= dt. AddDays (1-DT. Day);//Early this monthDateTime Endmonth = startmonth.addmonths (1). AddDays (-1);//Month End//DateTime endmonth = startmonth.adddays (dt. AddMonths (1)-DT).  DAYS-1); //Month EndDateTime Startquarter= dt. AddMonths (0-(dt. Month-1) %3). AddDays (1-DT. Day);//At the beginning of the quarterDateTime Endquarter = startquarter.addmonths (3). AddDays (-1);//At the end of the quarterDateTime startyear=NewDateTime (dt. Year,1,1);//early this yearDateTime endyear =NewDateTime (dt. Year, A, to);//End of year

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/guoyz_1/archive/2009/01/15/3785705.aspx

C # gets Date function classes for Monday and Sunday

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.