Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Find total weeks in a given month

0.00/5 (No votes)
26 Feb 2016 1  
Its needed to work with different factors of date. To find out total number of weeks in months is one of important task in it.

Introduction

For programmers, specially for whom who works on calendar based project, its always necessary to work with different factors of date. To find out total number of weeks in months is one of important task in it.

After plenty of search, i don't get expected result to find total number of weeks in a month. Then i tried my own idea and develop the code given below. This was working fine with me. If required, check it and analysis it.

Using the code

Find the code given below. I have tested this code and implemented in my applications. With some syntax changes, same will be implemented using C#.net

//
 Public Shared Function GetTotalWeeks(month As Integer) As Integer
 Dim today As Date = DateTime.Now
 Dim daysinmonth As Integer = DateTime.DaysInMonth(today.Year, month)
 Dim firstOfMonth As DateTime = New DateTime(today.Year, month, 1)
 Dim firstDayOfMonth As Integer = (firstOfMonth.DayOfWeek)
 Dim weeksInMonth As Integer = Math.Round((firstDayOfMonth + daysinmonth) / 7.0)
 Return (weeksInMonth)
 End Function
//

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here