We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Get the week number in year

hi, I want to know if there is a function that returns the number of a week in a certain year. for example: The week (from 24 june 2006 Till 30 june 2006) is the week #26 of the year 2006

3 Replies

JK Joy K George Syncfusion Team May 29, 2006 09:48 AM UTC

Hi Myra, Import the namespace System.Globalization and try the following code snippet. private void Button1_Click(object sender, EventArgs e){ CultureInfo myCI = new CultureInfo("en-US"); Calendar myCal = myCI.Calendar; // Gets the DTFI properties required by GetWeekOfYear. CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule; DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek; // Displays the number of the current week relative to the beginning of the year. Console.WriteLine("The CalendarWeekRule used for the en-US culture is {0}.", myCWR); Console.WriteLine("The FirstDayOfWeek used for the en-US culture is {0}.", myFirstDOW); Console.WriteLine("Therefore, the current week is Week {0} of the current year.", myCal.GetWeekOfYear(DateTime.Now, myCWR, myFirstDOW)); } Regards, Joy


MY Myra June 1, 2006 01:40 PM UTC

thanks joy!great!maybe u can help me doing the opposite work: function that gives the start and end day of a week number: so if I choose the first week of the year 2006, it will give me: 31/12/2006 --> 06/01/2006 if the week begins on saturday regards, Myra


JK Joy K George Syncfusion Team June 2, 2006 10:43 AM UTC

Hi Myra, If my understanding about your requirement is right you could achieve using the below code. int week = int.Parse(textBox1.Text); int year = int.Parse(textBox2.Text); DateTime dt = new DateTime(year, 1, 1); dt=dt.AddDays(week * 7); //it will add days to the starting date (eg 1/1/2006) MessageBox.Show(dt.ToString()); Regards, Joy

Loader.
Live Chat Icon For mobile
Up arrow icon