C # Gets the first day of the week, the last day, the first day of the month, and the last day

Source: Internet
Author: User

[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

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.