#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