65.9K
CodeProject is changing. Read more.
Home

Find total weeks in a given month

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.71/5 (9 votes)

Feb 26, 2016

CPOL
viewsIcon

11831

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
//