none
Get Week number for month

    Question

  •  Hello All,

    How to get the week number for month from date value?

    Thanks in advance.

    Friday, November 21, 2008 11:10 AM

Answers

  • This should be somewhat simpler, and this adjusts across cultures to account for the fact that some cultures consider Monday to be the first week, and some consider Sunday to be the first week.

    The first week of the month in this example can be a partial week (for instance, the first week of the month may only be a few days if the month starts on Wednesday.

    Hope this helps.

    public static int GetWeekOfMonth(DateTime date)  
    {  
        DateTime beginningOfMonth = new DateTime(date.Year, date.Month, 1);  
     
        while (date.Date.AddDays(1).DayOfWeek != CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)  
            date = date.AddDays(1);  
     
        return (int)Math.Truncate((double)date.Subtract(beginningOfMonth).TotalDays  / 7f) + 1;  

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Shobha P Tuesday, November 25, 2008 4:47 AM
    Friday, November 21, 2008 5:43 PM
    Moderator

All replies

  • Here is example:

    public static int GetWeekNumber(DateTime now) 
            { 
                CultureInfo ci = CultureInfo.CurrentCulture; 
                int weekNumber = ci.Calendar.GetWeekOfYear(now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); 
                return weekNumber; 
            } 

    Just pass DateTime.Now as a parameter to GetWeekNumber() method.

    Tomi Airaksinen - MCPD [Remember to click "mark as answered" when you get a correct reply to your question]
    • Proposed as answer by Tomi Airaksinen Friday, November 21, 2008 11:21 AM
    • Unproposed as answer by Shobha P Friday, November 21, 2008 11:25 AM
    Friday, November 21, 2008 11:21 AM
  • Hi Tomi,

    This method returs the week of the year. I want week for the month.

    Ex: Week number from 1-5, to which week my date belong to. if we consider starting day of the week is sunday.
    • Edited by Shobha P Friday, November 21, 2008 11:33 AM
    Friday, November 21, 2008 11:25 AM
  • Well, man, you determine the weekday of the first day of the month. Then you can calculate how many weeks there are.

    Week day number is like (int)DateTime.Now.DayOfWeek where Sunday = 0, Monday = 1, etc. The rest is very simple math.
    Friday, November 21, 2008 11:31 AM
  • Hi,

    Just check this link

    http://www.precharge.net/forums/asp/448-get-week-number.html

    You have to use the below code


     public static int WeekDay(DateTime dt) 
            { 
                // Set Year 
                int yyyy = dt.Year; 
                // Set Month 
                int mm = dt.Month; 
     
                // Set Day 
                int dd = dt.Day; 
                // Declare other required variables 
                int DayOfYearNumber; 
                int Jan1WeekDay; 
                int WeekDay; 
     
                int i, j, k, l; 
                int[] Mnth = new int[12] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; 
     
                // Set DayofYear Number for yyyy mm dd 
                DayOfYearNumber = dd + Mnth[mm - 1]; 
                // Increase of Dayof Year Number by 1, if year is leapyear and month is february 
                if ((IsLeapYear(yyyy) == true) && (mm == 2)) 
                    DayOfYearNumber += 1; 
                // Find the Jan1WeekDay for year 
                i = (yyyy - 1) % 100; 
                j = (yyyy - 1) - i; 
                k = i + i / 4; 
                Jan1WeekDay = 1 + (((((j / 100) % 4) * 5) + k) % 7); 
                // Calcuate the WeekDay for the given date 
                l = DayOfYearNumber + (Jan1WeekDay - 1); 
                WeekDay = 1 + ((l - 1) % 7); 
                return WeekDay; 
            } 
     
            public static bool IsLeapYear(int yyyy) 
            { 
     
                if ((yyyy % 4 == 0 && yyyy % 100 != 0) || (yyyy % 400 == 0)) 
                    return true
                else 
                    return false
            }  
     


    HTH

    Jaz
    • Proposed as answer by jaz.jaz Friday, November 21, 2008 11:33 AM
    Friday, November 21, 2008 11:33 AM
  • HI jaz,

    Have u tested this code? Its not returning the correct value.. :(
    Ex : 11/02/2008 is the 2nd week of the month, its returning 6 as weekday.
    Friday, November 21, 2008 11:50 AM

  • sorry for the mistake.  I have made some changes and i am getting correct result

    1 public static int WeekDay(DateTime dt) 
    2        { 
    3            // Set Year 
    4            int yyyy = dt.Year; 
    5            // Set Month 
    6            int mm = dt.Month; 
    7 
    8            // Set Day 
    9            int dd = dt.Day; 
    10            // Declare other required variables 
    11            int DayOfYearNumber; 
    12            int Jan1WeekDay; 
    13            int WeekDay; 
    14 
    15            int i, j, k, l; 
    16            int[] Mnth = new int[12] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; 
    17 
    18            // Set DayofYear Number for yyyy mm dd 
    19           // DayOfYearNumber = dd + Mnth[mm - 1]; 
    20            DayOfYearNumber = 1 + Mnth[mm - 1]; 
    21            // Increase of Dayof Year Number by 1, if year is leapyear and month is february 
    22            if ((IsLeapYear(yyyy) == true) && (mm == 2)) 
    23                DayOfYearNumber += 1; 
    24            // Find the Jan1WeekDay for year 
    25            i = (yyyy - 1) % 100; 
    26            j = (yyyy - 1) - i; 
    27            k = i + i / 4; 
    28            Jan1WeekDay = 1 + (((((j / 100) % 4) * 5) + k) % 7); 
    29            // Calcuate the WeekDay for the given date 
    30            l = DayOfYearNumber + (Jan1WeekDay - 1); 
    31            WeekDay = 1 + ((l - 1) % 7); 
    32 
    33            WeekDay = (dd + WeekDay) / 7; 
    34 
    35            return (WeekDay+1); 
    36        } 


    Thanks
    Jaz

    • Proposed as answer by jaz.jaz Friday, November 21, 2008 1:35 PM
    Friday, November 21, 2008 1:34 PM
  • This should be somewhat simpler, and this adjusts across cultures to account for the fact that some cultures consider Monday to be the first week, and some consider Sunday to be the first week.

    The first week of the month in this example can be a partial week (for instance, the first week of the month may only be a few days if the month starts on Wednesday.

    Hope this helps.

    public static int GetWeekOfMonth(DateTime date)  
    {  
        DateTime beginningOfMonth = new DateTime(date.Year, date.Month, 1);  
     
        while (date.Date.AddDays(1).DayOfWeek != CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)  
            date = date.AddDays(1);  
     
        return (int)Math.Truncate((double)date.Subtract(beginningOfMonth).TotalDays  / 7f) + 1;  

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Shobha P Tuesday, November 25, 2008 4:47 AM
    Friday, November 21, 2008 5:43 PM
    Moderator
  • Thanks a lot David. Its working :) :)
    Tuesday, November 25, 2008 4:47 AM
  • Why don't you get the Day of the month divide 7
    for example:
    the Day's value is 11, 11%7=1
    so it is in the second weed of the month
    This way does not depend on whether or not leap year

    Tuesday, November 25, 2008 5:05 AM
  • Hi David,

    David Can you help me in modifying your method to get week from the range 1 to 6 as we have in the calendar control

    like for january 2010 my dates are

    _ _ _ _ 1 2

    3 4 5 6 7 8

    ..............

    .............

    .............

    31 _ _ _ _ _

     

    if my selected date is 31st of january then i needed the Week number as 6

    [I were able to get number of week span for a month via help of http://stackoverflow.com/questions/9805/calculate-datetime-weeks-into-rows but no able to get current week number via date]

     


    Kamran Shahid Senior Software Engineer/Analyst (MCP,MCAD,MCSD.NET,MCTS,MCPD.net[web])
    Monday, August 23, 2010 8:31 AM

  • This should be somewhat simpler, and this adjusts across cultures to account for the fact that some cultures consider Monday to be the first week, and some consider Sunday to be the first week.

    The first week of the month in this example can be a partial week (for instance, the first week of the month may only be a few days if the month starts on Wednesday.

    Hope this helps.

    public static int GetWeekOfMonth(DateTime date)  
    {  
        DateTime beginningOfMonth = new DateTime(date.Year, date.Month, 1);  
     
        while (date.Date.AddDays(1).DayOfWeek != CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)  
            date = date.AddDays(1);  
     
        return (int)Math.Truncate((double)date.Subtract(beginningOfMonth).TotalDays  / 7f) + 1;  

    David Morton - http://blog.davemorton.net/

    Your weeknumber calculation if a bit too complex, this yields the same results and is a lot easier to the eyes:

     

    return (date.Subtract(beginningOfMonth).Days / 7) + 1;
    

     

    Monday, August 23, 2010 2:03 PM