Calculating The Weeks Of A Month With ColdFusion

Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

I'm taking a little break from blogging this week to try get on top of my work load. To fill the gap until then here are two little functions I wrote a while back for getting the weeks in a month.

These come in useful for projects where you have to interface with reporting systems with numbered weeks in a month. They are also useful for calendar apps.

Weeks In Month

This function calculates how many weeks are in a given month.

<cffunction name="weeksInMonth" output="false">
      <cfargument name="testDate" hint="Any date in the month you want to get number of weeks for">

      <!--- Variable to store the return week count --->
      <cfset var weekCount = 5><!--- min 4 max 6 --->
            
      <!--- Get the number of days in the month --->
      <cfset var numDays = daysinmonth(arguments.testDate)>
            
      <!--- Now get the month start date --->
      <cfset var dtMonthStart = createDate(year(arguments.testDate),month(arguments.testDate),1)>
            
      <!--- Get the offset of the first day in the month --->
      <cfset var firstDay = DayOfWeek(dtMonthStart)>
            
      <!--- Determine the number of weeks --->
      <cfif numDays EQ 28 AND firstDay EQ 1>
            <cfset weekCount = 4>
      <cfelseif (numDays EQ 30 AND firstDay EQ 7) OR (numDays EQ 31 AND firstDay GT 5)>
            <cfset weekCount = 6>
      </cfif>

      <cfreturn weekCount>
</cffunction>

Get The Current Week Number In The Month

Get the week number in a month of a given date.

<cffunction name="weekOfMonth" output="false">
      <cfargument name="testDate" hint="Any date in the week you want to get a week number for">

      <!--- Variable to store the return week number --->      
      <cfset var weekNumber = 1>
            
      <!--- Get current day --->
      <cfset var today = day(arguments.testDate)>
            
      <!--- Now get the month start date --->
      <cfset var dtMonthStart = createDate(year(arguments.testDate),month(arguments.testDate),1)>
            
      <!--- Get the day of the week the month starts on --->
      <cfset var firstDay = dayOfWeek(dtMonthStart)>

      <!--- Offset the first day by neg 1 --->      
      <cfset var offset = firstDay - 1>

      <!--- Calculate the week number --->
      <cfif today LTE (7 - offset)>
            <cfset weekNumber = 1>
      <cfelseif today LTE (14 - offset)>
            <cfset weekNumber = 2>
      <cfelseif today LTE (21 - offset)>
            <cfset weekNumber = 3>
      <cfelseif today LTE (28 - offset)>
            <cfset weekNumber = 4>
      <cfelseif today LTE (35 - offset)>
            <cfset weekNumber = 5>
      <cfelse>
            <cfset weekNumber = 6>
      </cfif>

      <cfreturn weekNumber>
</cffunction>

Testing

Heres a quick example for testing these functions.

<cfoutput>      
      weeks in month: #weeksInMonth(now())#<br/>
      Current Week: #weekOfMonth(now())#
</cfoutput>

Hope these come in useful. I should be back blogging next week once my work load levels off a bit (fingers crossed) :-).

Related Blog Postings

Reader Comments

Henry's Gravatar
Henry
Tuesday, August 14, 2012 at 12:22:33 PM Coordinated Universal Time

I think your functions would be better if they support weekstart being any day of the week.

Steven Neiland's Gravatar
Steven Neiland
Tuesday, August 14, 2012 at 1:22:32 PM Coordinated Universal Time

I've never had any need to use any other week start than Sunday.

I'm sure somewhere there probably is a need for that edge case but since I've never encountered it myself I have'nt allowed for it.

If I ever need to I'll add it here.

Alternatively if you want please feel free to post up a better solution. Always interested in seeing how other tackle a problem.

Steven Neiland's Gravatar
Steven Neiland
Tuesday, August 14, 2012 at 1:24:20 PM Coordinated Universal Time

Like I said this really is'nt a regular blog posting as I'm trying to take some time away from blogging.

This is more a share something useful kinda post rather than an in depth analysis.

Paul Mascari's Gravatar
Paul Mascari
Monday, October 1, 2012 at 10:13:26 AM Coordinated Universal Time

Steven,

Just found your functions and they helped me out a lot, thanks. Regarding Henry's comment, media companies, such as TV and radio stations, cable companies, etc, frequently use calendars that have weeks starting on Mondays for the purpose of planning and selling programs and advertising. My company deals with these on a regular basis.

http://en.wikipedia.org/wiki/Broadcast_calendar

Thanks, again.

Steven Neiland's Gravatar
Steven Neiland
Monday, October 1, 2012 at 10:17:18 AM Coordinated Universal Time

@Paul,
You're welcome. My next article in this series "Calculate Week Numbers From A Base Day" actually deals with this exact requirement.
Steven

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain advertisments or appear to be created for the purpose of link building, will not be published.

Archives Blog Listing