Click here to Skip to main content
15,893,588 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...

example

week number = 1

year = 2012

1/1/2012 8/1/2012


week number 48

year = 2011


1/2/2012 6/1/2012



I TRY THIS CODE

C#
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
Posted
Comments
anushripatil 10-Jan-12 5:55am    
http://stackoverflow.com/questions/1497586/how-can-i-calculate-find-the-week-number-of-a-given-date

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

i can simply achive that


by this,,,
C#
DateTime Firstday = dt.AddDays(-(int)dt.DayOfWeek);
       DateTime Endaday = Firstday.AddDays(6);
 
Share this answer
 
 
Share this answer
 
v6
Hi,

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.
 
Share this answer
 
v3
Hi,

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

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