locked
Number of Weeks in Month

    Question

  • I m in real trouble,

    My problem is to calculate how many weeks have a month.And then What Days including in that Weeks.For example,If We Select month March, April,... From Calender ,Then it Show that the month March, April, ... Consists of 4 Weeks suppose,there is a DropDownlist which Fill Dynamically Showning the Number of Weeks of the Selected Month in this Format :
    WEEK1
    WEEK2
    WEEK3
    WEEK4 (If there are 4 Weeks)

    When We Select Week1 From the DropDown Then it will show days including in that Selected Week.Like Tuesday,Wednesday,Thursday etc (i-e days including in that Selected Week1)

    Same is If We Select Week4 then it will show days in that Week.

    I need Code of this Problem in VB.NET or C#.
    Plz help me.

    Waiting for ur positive Response.

    Thank you

    Madasamy

    Monday, November 26, 2012 11:48 AM

Answers

  • Your right.  I needed to use the ceiling function.

                DateTime today = DateTime.Now;
                //extract the month
                int daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);
                DateTime firstOfMonth = new DateTime(today.Year, today.Month, 1);
                //days of week starts by default as Sunday = 0
                int firstDayOfMonth = (int)firstOfMonth.DayOfWeek;
                int weeksInMonth = (int)Math.Ceiling((firstDayOfMonth + daysInMonth) / 7.0);
                Console.WriteLine("5 weeks in month");


    jdweng

    Monday, November 26, 2012 1:56 PM

All replies

  • since a week is 7 days and a month is at least 28 days, there is always a minimum of 4 weeks in a month.

    If partial week are counted as a week, then the question is :

     is there 5 weeks in that months .. 

     TIt can also be 6 weeks, if the month has 31 days, and start a Saturday, the 31st will be Sunday 6 week later

    You need to define what you are calling a week

    Unless you are using a different calendar?

    Monday, November 26, 2012 12:01 PM
  • same the particular month is 6 or 5 weeks , display as Week1, Week2...Week6 in dropdownlist

    Monday, November 26, 2012 12:14 PM
  • The algorithm is very simple.  Use the DayOfWeek() method for first day of the month and then add the number of days in the month and divide by 7.

                DateTime today = DateTime.Now;
                //extract the month
                int daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);
                DateTime firstOfMonth = new DateTime(today.Year, today.Month, 1);
                //days of week starts by default as Sunday = 0
                int firstDayOfMonth = (int)firstOfMonth.DayOfWeek;
                int weeksInMonth = ((int)(firstDayOfMonth + daysInMonth) / 7);
                Console.WriteLine("5 weeks in month");


    jdweng

    Monday, November 26, 2012 1:09 PM
  • This code will return the Week of the year for a date:

    var cal = new System.Globalization.Calendar();
    var woyStart = cal.GetWeekOfYear(dateTimeMonthStart,
        CalendarWeekRule.FirstDay,
        DayOfWeek.Sunday
    );
    var woyEnd = cal.GetWeekOfYear(dateTimeMonthEnd,
        CalendarWeekRule.FirstDay,
        DayOfWeek.Sunday
    );


    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    Monday, November 26, 2012 1:18 PM
  • @Joel:

    Your algorithm won't handle the occasional "6-week month", when the 1st is on Saturday and the month has 30 or 31 days, or the 1st is on Friday and the month has 31 days.


    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    Monday, November 26, 2012 1:21 PM
  • Your right.  I needed to use the ceiling function.

                DateTime today = DateTime.Now;
                //extract the month
                int daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);
                DateTime firstOfMonth = new DateTime(today.Year, today.Month, 1);
                //days of week starts by default as Sunday = 0
                int firstDayOfMonth = (int)firstOfMonth.DayOfWeek;
                int weeksInMonth = (int)Math.Ceiling((firstDayOfMonth + daysInMonth) / 7.0);
                Console.WriteLine("5 weeks in month");


    jdweng

    Monday, November 26, 2012 1:56 PM