Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi ..

how i get start and end date of week..

i will pass .. Week number and year ...
or Any Date

and it return start and end date of that week...


week number = 1

year = 2012

1/1/2012 8/1/2012

week number 48

year = 2011

1/2/2012 6/1/2012


DateTime strtdt = DateTime.ParseExact(tx_dt.Text, "dd/MM/yyyy", null);

       GregorianCalendar cal = new GregorianCalendar(GregorianCalendarTypes.Localized);
       int week = cal.GetWeekOfYear(strtdt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

       int year = strtdt.Year;

       DayOfWeek day = DayOfWeek.Monday;
       DateTime startOfYear = new DateTime(year, 1, 1);

       // The +7 and %7 stuff is to avoid negative numbers etc.
       int daysToFirstCorrectDay = (((int)day - (int)startOfYear.DayOfWeek) + 7) % 7;

       DateTime FirstDay = startOfYear.AddDays(7 * (week - 1) + daysToFirstCorrectDay);
       DateTime Endaday = startOfYear.AddDays(7 * (week - 1) + daysToFirstCorrectDay).AddDays(6);

       TextBox4.Text = FirstDay.ToString("dd/MM/yyyy");
       TextBox5.Text = Endaday.ToString("dd/MM/yyyy");

but it working fine only for year .. .when previoues year week is 53...
its working fine for 2011 , 2012 .. but when i m checking for 2013 it giving next week date
anushripatil 10-Jan-12 5:55am

thnks for all ... i just going to wrong side ...

i can simply achive that

by this,,,
DateTime Firstday = dt.AddDays(-(int)dt.DayOfWeek);
       DateTime Endaday = Firstday.AddDays(6);

The year 2013 starts from Tuesday. So when you take the first week by taking first Monday, your code returns the second week.
As per you code logic, I understood you calculated first Monday as first week.
So it returns the the second week of dates.

Here you can get a method to get first day of the given week & year.
Just change the logic as your need

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900