# Get Week number for month

### Question

•  Hello All,

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

Friday, November 21, 2008 11:10 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/
• Marked as answer by Tuesday, November 25, 2008 4:47 AM
Friday, November 21, 2008 5:43 PM

### 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 Friday, November 21, 2008 11:21 AM
• Unproposed as answer by 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 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,

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 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 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 Tuesday, November 25, 2008 4:47 AM
Friday, November 21, 2008 5:43 PM
• 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
• Well that was my first though too,

```public static int getWeekofMonth(DateTime dtn){
int maths = dtn.Day % 7;
maths++;
return maths;
}```

shouldn't that be it?

mfg

Con

Tuesday, July 14, 2015 9:22 AM
• That works for US weeknumbers only. The whole issue is that ISO weeknumbers use different ways to figure out the first week of the year and consequently also the first week of a month.

In The Netherlands, for example, the first week of the year (or month) is the first week that has at least 4 days of said months and starts counting from MONDAY not from SUNDAY.

So the internationally correct version of this method is not easy to create and involves a lot of CultureInfo handling.

My blog: blog.jessehouwing.nl

Tuesday, July 14, 2015 12:48 PM
• Here is the best solution. You can change a Culture if you want.

```public static int GetWeekOfYear(DateTime date)
{
if (date == null)
return 0;

DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
Calendar cal = dfi.Calendar;

return cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
}

public static int GetWeekOfMonth(DateTime date)
{
if (date == null)
return 0;

return GetWeekOfYear(date) - GetWeekOfYear(new DateTime(date.Year, date.Month, 1)) + 1;
}```

Monday, November 23, 2015 12:28 PM
• I agree that this is the best approach.  It allows leaving all the finer points of beginning DOW for month, etc. to the Microsoft Globalization and Classes like CultureInfo.  The other solutions above are very helpful but each one produced incorrect results for months like July, October, or December where the Week 5 or Week 6 issue comes up.  This solution correctly provides the week of the month for all scenarios.  It is also very simple conceptually.

(Week of year for date) -  (Week of year for first day of month containing the date) equals (Week of Month containing date).    Perhaps check the date values and format before passing it would be helpful.

Web Developer

• Edited by Sunday, February 21, 2016 4:00 PM Improved comment.
Sunday, February 21, 2016 3:56 PM