[CSharp] View plaincopyprint? Public classDatetimetool {/// <summary> ///Gets the first day of the week on which the specified date is, and Sunday is the first day/// </summary> /// <param name= "DateTime" ></param> /// <returns></returns> Public Staticdatetime Getdatetimeweekfirstdaysun (datetime datetime) {datetime firstweekday=DateTime.Now; Try { //get the day of the week and subtract the number of days from the current date intWeeknow =Convert.ToInt32 (Datetime.dayofweek); intDaydiff = (-1) *Weeknow; Firstweekday=datetime.adddays (Daydiff); } Catch { } returnFirstweekday; } /// <summary> ///Gets the first day of the week on which the specified date is, and Monday is the first day/// </summary> /// <param name= "DateTime" ></param> /// <returns></returns> Public Staticdatetime Getdatetimeweekfirstdaymon (datetime datetime) {datetime firstweekday=DateTime.Now; Try { intWeeknow =Convert.ToInt32 (Datetime.dayofweek); //Monday is the first day, Weeknow equals 0 o'clock and pushes forward 6 days. Weeknow = (Weeknow = =0? (7-1): (Weeknow-1)); intDaydiff = (-1) *Weeknow; Firstweekday=datetime.adddays (Daydiff); } Catch { } returnFirstweekday; } /// <summary> ///gets the last day of the week on which the specified date is, and Saturday is the last day/// </summary> /// <param name= "DateTime" ></param> /// <returns></returns> Public Staticdatetime Getdatetimeweeklastdaysat (datetime datetime) {datetime lastweekday=DateTime.Now; Try { intWeeknow =Convert.ToInt32 (Datetime.dayofweek); intDaydiff = (7-Weeknow)-1; Lastweekday=datetime.adddays (Daydiff); }Catch { } returnLastweekday; } /// <summary> ///gets the last day of the week on which the specified date is, and Sunday is the last day/// </summary> /// <param name= "DateTime" ></param> /// <returns></returns> Public Staticdatetime Getdatetimeweeklastdaysun (datetime datetime) {datetime lastweekday=DateTime.Now; Try { intWeeknow =Convert.ToInt32 (Datetime.dayofweek); Weeknow= (Weeknow = =0?7: Weeknow); intDaydiff = (7-Weeknow); Lastweekday=datetime.adddays (Daydiff); } Catch { } returnLastweekday; } /// <summary> ///gets the first day of the month for the specified date/// </summary> /// <param name= "DateTime" ></param> /// <returns></returns> Public Staticdatetime getdatetimemonthfirstday (datetime datetime) {if(DateTime = =NULL) {DateTime=DateTime.Now; } return NewDateTime (Datetime.year, DateTime.Month,1); } /// <summary> ///gets the last day of the specified month/// </summary> /// <param name= "DateTime" ></param> /// <returns></returns> Public Staticdatetime getdatetimemonthlastday (datetime datetime) {intDay =Datetime.daysinmonth (Datetime.year, datetime.month); return NewDateTime (Datetime.year, DateTime.Month, day); } }
C # Gets the first day of the week, the last day, the first day of the month, and the last day