-3

I need list of weeks with starting and ending dates by giving int year and int month,

Example Result,

Week1 = 7/1/2012 to 7/1/2012

Week2 = 7/2/2012 to 7/8/2012

Week3 = 7/9/2012 to 7/15/2012

Week4 = 7/16/2012 to 7/22/2012

Week5 = 7/23/2012 to 7/29/2012

Week6 = 7/30/2012 to 7/31/2012

user1500529
  • 15
  • 2
  • 3
  • 3
    Why does the first week only have one day? Because it's a Sunday? You need to be more precise in your question. – Jon Skeet Jul 13 '12 at 06:46
  • the 1st week have only 1 day because first day of week is Monday, its basically the calender view if we see our calender in windows – user1500529 Jul 13 '12 at 06:57
  • Well, obviously he put `July` and `2012` as parameters. Those are weeks of July. What I do not like about this question is that he didn't show any effort to solve it himself... – Michal B. Jul 13 '12 at 06:58
  • 3
    @user1500529: Right, you didn't say any of that before though. Bear in mind that different cultures and contexts use different "first day of week" ideas. – Jon Skeet Jul 13 '12 at 07:15

2 Answers2

6

Something like this should work:

// using System.Globalization;
var calendar = CultureInfo.CurrentCulture.Calendar;
var firstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
var weekPeriods =
Enumerable.Range(1, calendar.GetDaysInMonth(year, month))
          .Select(d =>
          {
              var date = new DateTime(year, month, d);
              var weekNumInYear = calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, firstDayOfWeek);
              return new { date, weekNumInYear };
          })
          .GroupBy(x => x.weekNumInYear)
          .Select(x => new { DateFrom = x.First().date, To = x.Last().date })
          .ToList();

Of course you can change the Culture (here I have used the CurrentCulture).

digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • Thank you so much as its working great. i need one more favour from you. can you please also let me know how i will get the result from this var object? or is possible save these value in a array from where i can access them easily. Thankx – user1500529 Jul 13 '12 at 07:39
  • Check my edit. I added a `ToList()` at the end so you get a list of periods :) – digEmAll Jul 13 '12 at 07:51
  • Thank you so much Dear Specially for list conversion :D – user1500529 Jul 13 '12 at 07:57
2

check this one and edit according to your need

// Get the weeks in a month

        DateTime date = DateTime.Today;
        // first generate all dates in the month of 'date'
        var dates = Enumerable.Range(1, DateTime.DaysInMonth(date.Year, date.Month)).Select(n => new DateTime(date.Year, date.Month, n));
        // then filter the only the start of weeks
        var weekends = (from d in dates
                       where d.DayOfWeek == DayOfWeek.Monday
                       select d).ToList();
        foreach (var d in weekends)
        {
            Console.WriteLine(d);
        }
        Console.Write(weekends);
        Console.ReadLine();

hope it help you.

you can also take a look here for other stuff HERE

Community
  • 1
  • 1
Sunny
  • 3,185
  • 8
  • 34
  • 66