Home  »  Code Snippets  »  C#.Net  »

get Week of the Month in C#.Net

 Posted By : Manoranjan SahooPosted Date : 04/04/2012 10:11:05 AM Category : C#.Net Points : 3

Sometimes we need to find the week of the month in our program. Once i needed this one and try to achieve this and finally i achieved this with following code : 

Code:

public static int GetWeekOfMonth(DateTime date)

{     

            DateTime beginningOfMonth = new DateTime(date.Year, date.Month, 1);  

           while ((int)date.Date.AddDays(1).DayOfWeek != 0)            

                 date = date.AddDays(1); 

           return (int)Math.Truncate((double)date.Subtract(beginningOfMonth).TotalDays / 7f) + 1;  

}

 

Hope you all like this code.

Thank you.

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.