Click here to Skip to main content
15,906,574 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Just can't find a solution for my problem..

I have a weeknumber, e.g. 13 and I want date in between this week and days also...
the output to be:

Week 13 (2012):
25 March Sunday
26 March Monday
27 March Tuesday
28 March Wednesday
29 March Thursday
30 March Friday
31 March Saturday
Posted
Comments
Andy411 30-Mar-12 7:45am    
Did I understand it: You only have the year and the week and you want to know exact dates of the week?
Have you looked at System.Globalization.Calendar ?
atul tr 30-Mar-12 7:50am    
yeah i have only week number of the year and i want days name and dates

You can use the Week class of the Time Period Library for .NET[^]:
C#
// ----------------------------------------------------------------------
public void DaysOfWeekSample()
{
  Week week = new Week( 2012, 13 );
  foreach ( Day day in week.GetDays() )
  {
    Console.WriteLine( day );
  }
} // DaysOfWeekSample


The class supports also the ISO 8601 week numbering:
C#
// ----------------------------------------------------------------------
public void DaysOfWeekSample()
{
  TimeCalendar calendar = new TimeCalendar(
    new TimeCalendarConfig { YearWeekType = YearWeekType.Iso8601 } );
  Week week = new Week( 2012, 13, calendar );
  foreach ( Day day in week.GetDays() )
  {
    Console.WriteLine( day );
  }
} // DaysOfWeekSample
 
Share this answer
 
A bit quick and dirty, but hopefully a starting point for you:

C#
int searchYear = 2012;
int searchWeek = 13;

DateTime searchStartDate = new DateTime(searchYear, 1, 1) - TimeSpan.FromDays(7);

Calendar calendar = CultureInfo.CurrentCulture.Calendar;

int weekOfYear = 0;

do
{
    searchStartDate += TimeSpan.FromDays(7);
    weekOfYear = calendar.GetWeekOfYear(searchStartDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

} while (weekOfYear != searchWeek);

// now searchStartDate is in the week you are looking for


Disadvantage of this solution is, that you have to loop through all the weeks.

Pay attention on the rules defining the week.
 
Share this answer
 
v2
Try my solution:
const int weekNumber = 9;
const int daysNumber = weekNumber * 7;
var dtNow = DateTime.Today;
var firstDateInYear = new DateTime(dtNow.Year, 1, 1);
var lastDayInWeek = firstDateInYear.AddDays(daysNumber);
var firstDayInWeek = lastDayInWeek.AddDays(-7);

for (var day = firstDayInWeek; day <= lastDayInWeek; day = day.AddDays(1))
{
     Console.WriteLine(day.Day + " " + day.Month + " " + day.DayOfWeek);
}
 
Share this answer
 
It very much depends on your culture on what calendar week you have.
Continental Europe (e.g. ISO8601 based) is different to UK and US, and others...

This gives the first date of a given week, depending of your culture settings:
C#
  1  private static DayOfWeek FirstDay
  2  { get { return CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek; } }
  3  private static CalendarWeekRule WeekRule
  4  { get { return CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule; } }
  5  private static int GetWeek(DateTime date)
  6  {
  7      return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.Date, WeekRule, FirstDay);
  8  }
  9  private static DateTime FirstDateOfWeek(DateTime date)
 10  {
 11      return date.Date.AddDays(-((7 + date.Date.DayOfWeek - FirstDay) % 7));
 12  }
 13  private static DateTime FirstDateOfWeek(int year, int week)
 14  {
 15      DateTime dt = new DateTime(year, 1, 1);
 16      if (GetWeek(dt) == 1) dt = dt.AddDays(-7);
 17      return FirstDateOfWeek(dt.AddDays(week * 7));
 18  }
 19  public static IEnumerable<DateTime> WeekDaysOfWeek(DateTime date)
 20  {
 21      DateTime firstDayOfWeek = FirstDateOfWeek(date);
 22      foreach (int d in Enumerable.Range(0, 7)) yield return firstDayOfWeek.AddDays(d);
 23  }
 24  public static IEnumerable<DateTime> WeekDaysOfWeek(int year, int day)
 25  {
 26      return WeekDaysOfWeek(FirstDateOfWeek(year, day));
 27  }


Usage:
C#
 57  var culture = CultureInfo.CurrentCulture; // or new CultureInfo("....");
 58  Console.WriteLine("Today = {0}", DateTime.Today.ToString("ddd yyyy-MM-dd"));
 59  Console.WriteLine("Rule  = {0}", WeekRule);
 60  Console.WriteLine("Day1  = {0}", FirstDay);
 61  Console.WriteLine("Week  = {0}", GetWeek(DateTime.Today));
 62  
 63  foreach (var item in WeekDaysOfWeek(2012, GetWeek(DateTime.Today)))
 64  {
 65      Console.WriteLine("{0}", item.ToString("ddd yyyy-MM-dd"));
 66  }


Output:

Today = Fr 2012-03-30
Rule  = FirstFourDayWeek
Day1  = Monday
Week  = 13
Mo 2012-03-26
Di 2012-03-27
Mi 2012-03-28
Do 2012-03-29
Fr 2012-03-30
Sa 2012-03-31
So 2012-04-01
 
Share this answer
 
v2

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