C # obtain the start date and end date of the specified week in the specified year

Source: Internet
Author: User

Last year, I wrote a statistical function, which is weekly statistics.

Use the datepart (wk, createtime), datepart (Week, '1970-01-01 '), set datefirst 7, and other methods in SQL to obtain the number of current weeks combined with group.

I found a section on the Internet when I showed it at the front-end.CodeI noted the start date and end date of the week, but I didn't pay much attention to it at that time. I received an email yesterday and said, "The statistical results of the future date are also displayed, WEEK 1, 10th was displayed on the 3-11 to 3-17, but the query date was only 3 on the day of the year.

Maybe there is a problem with the code I found on the Internet. Maybe I won't use the wrong parameter. In short, the number of weeks and the start date cannot match the database.

Then I will rewrite a method that corresponds to the default settings of the ms SQL database to get the specified year's specified week start and end date.

Some of the Code comes from the code originally found on the Internet. My processing is somewhat different from the previous logic.

Use the returned result when using the function. Otherwise, the returned date may be incorrect when the number of cycles is incorrect.

The Code is as follows:

 1           ///   <Summary>  2           ///  Obtains the start and end dates of a specified number of weeks. The start date is Sunday. 3           ///   </Summary>  4           ///   <Param name = "year">  Year  </Param>  5           ///   <Param name = "Index">  Weeks  </Param>  6           ///   <Param name = "first"> When this method is returned, it contains the system. datetime value of the week's start date specified by year and index. If it fails, it is system. datetime. minvalue.  </Param>  7           ///   <Param name = "last">  When this method is returned, it contains the system. datetime value of the end date of the week specified by year and index. If it fails, it is system. datetime. minvalue.  </Param>  8           ///   <Returns> </returns>  9           Public   Static  Bool Getdaysofweeks ( Int Year, Int Index, Out Datetime first, Out  Datetime last)  10   {  11 First = Datetime. minvalue;  12 Last = Datetime. minvalue;  13               If (Year < 1700 | Year> 9999  )  14   {  15                   //  "The year exceeds the upper limit"  16                   Return   False  ;  17   }  18               If (Index <1 | Index> 53  )  19   {  20                   //  "Weekly error"  21                   Return   False  ;  22   }  23 Datetime startday = New Datetime (year,1 , 1 ); //  The first day of the year  24 Datetime endday = New Datetime (Year + 1 , 1 , 1 ). Addmilliseconds (- 1  );  25               Int Dayofweek = 0 ;  26               If (Convert. toint32 (startday. dayofweek. tostring ( "  D  " )> 0  )  27 Dayofweek = convert. toint32 (startday. dayofweek. tostring ( "  D  " )); //  The first day of the year is the day of the week  28              If (Dayofweek = 7 ) {Dayofweek = 0  ;}  29               If (Index = 1  )  30   {  31 First = Startday;  32                   If (Dayofweek = 6 )  33   {  34 Last = First;  35   }  36                   Else  37   {  38 Last = startday. adddays (( 6 - Dayofweek ));  39  }  40   }  41               Else  42   {  43 First = startday. adddays (( 7 -Dayofweek) + (index- 2 )* 7 ); //  Index week start date  44 Last = first. adddays (6  );  45                   If (Last> Endday)  46   {  47 Last = Endday;  48   }  49   }  50               If (First> endday)//  Startdayofweeks is not within this year  51   {  52                   //  "The number of weeks entered exceeds the maximum number of weeks of the current year ";  53                   Return   False  ;  54   }  55               Return   True  ; 56 }

Modified statistics:

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.