Click here to Skip to main content
15,747,637 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to get the week number of date. For 04-11-2012, week number is 1 and for 11-11-2012 week number as 2. Please anyone help me.
Thank you.
Updated 6-Feb-19 20:06pm
OriginalGriff 1-Nov-12 8:04am    
That can be quite complex - the ISO standard does not define when date is in what week (as it does for week number of year) so to a certain extent, it is going to depend on what rules you need to apply.
What day of the week does your week start? Sat, Sun, or Mon?
Is 1st of the month always in week one? Or does it follow the week of year rule and depend on the date of the Thursday?

You really don't need to write much code or do calculation for that. You can use
System.Globalization.CultureInfo.Calendar object. It has GetWeekOfYear method.

System.Globalization.CultureInfo cult_info = System.Globalization.CultureInfo.CreateSpecificCulture("no");

System.Globalization.Calendar cal = cult_info.Calendar; 
int weekCount = cal.GetWeekOfYear(date, cult_info.DateTimeFormat.CalendarWeekRule, cult_info.DateTimeFormat.FirstDayOfWeek);


Hope that helps. If it does then mark the answer as solution/upvote.

Share this answer
Zukiari 1-Nov-12 9:06am    
Here I'm getting the week no in year. I need for the month.
Thank you
DateTime dt=DateTime.Now;//yourdatetime
int weekOfMonth=(dt.Day + ((int)dt.DayOfWeek)) / 7 + 1;
Share this answer
Thomas Daniels 1-Nov-12 7:55am    
Good answer, +5!
OriginalGriff 1-Nov-12 8:07am    
So, what week does it have December 4th this year? :laugh:
OriginalGriff 1-Nov-12 8:06am    
Sorry, but that is not a good solution.
For your solution, 4th December 2012 is in week 1, where a calendar will probably put it in week 2...

Sorry, but it's a 1 from me.
Sanjay K. Gupta 1-Nov-12 8:10am    
Yes, you are right.
may be the solution
(dt.Day + ((int)dt.DayOfWeek)) / 7 + 1
Zukiari 1-Nov-12 8:33am    
It getting correct. Can i count the week from monday, that is, for 07-01-2013 it is in second week but 1st monday of the month. I want to get the no as 1.
Private Function WeekNumberOfMonth(ByVal Dt As Date) As Integer
       Dim weekNumber As Integer = Math.Floor(Dt.Day / 7) + 1
       Return weekNumber
   End Function

By Elian Fernandez
Share this answer
The code below give the result what I want exactly.

if (dtime.Day <= 7)
            count = 1;
        else if (dtime.Day > 7 && dtime.Day <= 14)
            count = 2;
        else if (dtime.Day > 14 && dtime.Day <= 21)
            count = 3;
        else if (dtime.Day > 21 && dtime.Day <= 28)
            count = 4;
        else if (dtime.Day > 28)
            count = 5;

Thank You.
Share this answer
Redundant code with exact solution as below Method/Function based on passed date,

private static int GetWeekNumberOfMonth(DateTime date)
           // One Solution
           decimal numberofday = date.Day;
           decimal d = (Math.Floor(numberofday / 7)) + 1;

           if ((numberofday) % 7 == 0)
              return Convert.ToInt32((d)) - 1;
           return Convert.ToInt32(d);

Share this answer
CHill60 7-Feb-19 4:50am    
A word of warning - you will get a better reception when you post solutions to old posts like this if you explain a bit more about why your solution is better than the others.
Something like this would do it - "Note with the other solutions that the 7th, 14th, 21st and 28th of each month are returned as week 2, 3, 4 and 5 instead of 1, 2, 3, 4. This adjustment caters for that"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900