-1

Hi I want to list date by week number for whole year so that output is like

Date          Week#    
21-03-2015      3
22-03-2015      3
23-03-2015      3
24-03-2015      3
25-03-2015      3
26-03-2015      3
27-03-2015      3
....
....
.....
21-12-2015      53
22-12-2015      53
23-12-2015      53
24-12-2015      53
25-12-2015      53
26-12-2015      53
27-12-2015      53

Following code will only

   public static DateTime[] WeekDays(int Year, int WeekNumber)
    {
        DateTime start = new DateTime(Year, 1, 1).AddDays(7 * WeekNumber);
        start = start.AddDays(-((int)start.DayOfWeek));
        return Enumerable.Range(0, 7).Select(num => start.AddDays(num)).ToArray();
    }

Please let me know how to add a second range for (0, 54) so it lists date for whole year by there week number. Thanks

J. Davidson
  • 3,297
  • 13
  • 54
  • 102
  • Duplicate? http://stackoverflow.com/questions/11154673/get-the-correct-week-number-of-a-given-date or http://stackoverflow.com/questions/1497586/how-can-i-calculate-find-the-week-number-of-a-given-date or http://stackoverflow.com/questions/7918593/how-can-i-determine-the-week-number-of-a-certain-date – Soner Gönül Aug 05 '15 at 07:26
  • Also NodaTime has `WeekOfWeekYear` property which is ISO-8601 standard – Soner Gönül Aug 05 '15 at 07:30
  • @soner read my question. Those duplicates you mentioned are asking for one particular date or week. I am asking for a list of whole year by week number. They are not same – J. Davidson Aug 05 '15 at 07:33
  • Then you can use _these_ duplicates while you iterating all days in a year. That's not that hard. – Soner Gönül Aug 05 '15 at 07:35
  • @soner read post again.......I tried to add second Range(0, 54) didnt work. So my question is how how to get the second range in it. – J. Davidson Aug 05 '15 at 07:37

1 Answers1

0

Try this, it may help you to get list of date and week number

public class DateAndWeek
{
    public DateTime Date { get; set; }
    public int WeekNumber { get; set; }
}
private List<DateAndWeek> GetListOfDateAndWeek(int startYear, int startMonth, int startDay)
{
    List<DateAndWeek> dateTimes = new List<DateAndWeek>();
    var week = 0;
    var dateTimeStart = new DateTime(startYear, startMonth, startDay);
    for (int i = 0; i < 365; i++)
    {
       var dateTime = dateTimeStart.AddDays(i);
       if (dateTime.DayOfWeek == DayOfWeek.Monday)
       {
          week++;
       }
       dateTimes.Add(new DateAndWeek { Date = dateTime, WeekNumber = week });
    }
    return dateTimes;
}

Use in this way

var list = GetListOfDateAndWeek(2015, 1, 1);

You have to customized your output according to your need.

NASSER
  • 5,900
  • 7
  • 38
  • 57