Click here to Skip to main content
15,891,943 members
Articles / DevOps
Tip/Trick

Find total weeks in a given month

Rate me:
Please Sign up or sign in to vote.
2.71/5 (9 votes)
26 Feb 2016CPOL 11.2K   1   3
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

C++
//
 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
India India
I am working as an application developer for last ten years. I am mostly develop applications in ASP.NET. But, also I work in Android and Java.

Currently I am working in a company as a Sr. Application Developer. I have a team of developers and I am playing a role of team leader.

Also, I assist academic projects based on IEEE papers for ME for last ten years.

I believed on a logic development and not on a programming language. I think that, if some one was strong in logic development then he/she can easily develop an application in any programming language.

I am working on tools/domains like, image processing, networking, cloud computing, etc. Out of them, cloud computing is my favorite working domain.

Comments and Discussions

 
Questionneeds work Pin
JRickey29-Feb-16 11:16
professionalJRickey29-Feb-16 11:16 
QuestionSorry, but.... Pin
Wombaticus26-Feb-16 7:40
Wombaticus26-Feb-16 7:40 
QuestionWhy rounding? Pin
Philippe Mori26-Feb-16 6:09
Philippe Mori26-Feb-16 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.