C # Gets the first and last day of the week on which a date is located

Source: Internet
Author: User

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceWyfclass { Public classTools {/// <summary>  ///get the first day of the week (first day of Sunday)/// </summary>  /// <param name= "datetime" ></param>  /// <returns></returns>   Publicdatetime Getweekfirstdaysun (datetime datetime) {//Sunday for the first day    intWeeknow =Convert.ToInt32 (DateTime.      DayOfWeek); intDaydiff = (-1) *Weeknow; //first day of the week    stringFirstDay = DateTime. AddDays (Daydiff). ToString ("YYYY-MM-DD"); returnConvert.todatetime (FirstDay); }    /// <summary>  ///get the first day of the week (first day of Monday)/// </summary>  /// <param name= "datetime" ></param>  /// <returns></returns>   Publicdatetime Getweekfirstdaymon (datetime datetime) {//Monday for the first day    intWeeknow =Convert.ToInt32 (DateTime.        DayOfWeek); //because it is the first day of Monday, so to judge Weeknow equals 0 o'clock, to push forward 6 days. Weeknow = (Weeknow = =0? (7-1): (Weeknow-1)); intDaydiff = (-1) *Weeknow; //first day of the week    stringFirstDay = DateTime. AddDays (Daydiff). ToString ("YYYY-MM-DD"); returnConvert.todatetime (FirstDay); }    /// <summary>  ///get the last day of the week (Saturday is the last day)/// </summary>  /// <param name= "datetime" ></param>  /// <returns></returns>   Publicdatetime Getweeklastdaysat (datetime datetime) {//Saturday is the last day    intWeeknow =Convert.ToInt32 (DateTime.      DayOfWeek); intDaydiff = (7-Weeknow)-1; //last day of the week    stringLastday = DateTime. AddDays (Daydiff). ToString ("YYYY-MM-DD"); returnConvert.todatetime (Lastday); }    /// <summary>  ///get the last day of the week (Sunday is the last day)/// </summary>  /// <param name= "datetime" ></param>  /// <returns></returns>   Publicdatetime Getweeklastdaysun (datetime datetime) {//Sunday is the last day    intWeeknow =Convert.ToInt32 (DateTime.      DayOfWeek); Weeknow= (Weeknow = =0?7: Weeknow); intDaydiff = (7-Weeknow); //last day of the week    stringLastday = DateTime. AddDays (Daydiff). ToString ("YYYY-MM-DD"); returnConvert.todatetime (Lastday); }  }  }  

C # Gets the first and last day of the week on which a date is located

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.