Ask Ben: Getting The Date Based On The Year And Week In ColdFusion

Posted May 22, 2007 at 1:04 PM by Ben Nadel

Tags: ColdFusion, Ask Ben

I am working on a calendar system and I need to figure out what dates I am working with based on weeks of the year. I know that I can use the week function to get the week of a current date, but I cannot figure out how to go the other way. Any help????

Getting a date based on a given week and year value is easy, but it has some interesting points to discuss. For instance, if you look at a given week, what day are you looking for? I assume you want the first day of that week. But, if you are looking at the first day of the week, then for week ONE of many years, that date will actually be in December of the previous year (as January 1st is not guaranteed to fall on a Sunday). I am not saying any of this is good or bad; I am just saying that it has to be taken into account and might influence the end solution.

That being said, assuming we just want to get the date of the first day of the week for a given week in a given year, here is a ColdFusion user defined function that will do this:

  • <cffunction
  • name="GetDateByWeek"
  • access="public"
  • returntype="date"
  • output="false"
  • hint="Gets the first day of the week of the given year/week combo.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Year"
  • type="numeric"
  • required="true"
  • hint="The year we are looking at."
  • />
  •  
  • <cfargument
  • name="Week"
  • type="numeric"
  • required="true"
  • hint="The week we are looking at (1-53)."
  • />
  •  
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = StructNew() />
  •  
  •  
  • <!---
  • Get the first day of the year. This one is
  • easy, we know it will always be January 1st
  • of the given year.
  • --->
  • <cfset LOCAL.FirstDayOfYear = CreateDate(
  • ARGUMENTS.Year,
  • 1,
  • 1
  • ) />
  •  
  • <!---
  • Based on the first day of the year, let's
  • get the first day of that week. This will be
  • the first day of the calendar year.
  • --->
  • <cfset LOCAL.FirstDayOfCalendarYear = (
  • LOCAL.FirstDayOfYear -
  • DayOfWeek( LOCAL.FirstDayOfYear ) +
  • 1
  • ) />
  •  
  • <!---
  • Now that we know the first calendar day of
  • the year, all we need to do is add the
  • appropriate amount of weeks. Weeks are always
  • going to be seven days.
  • --->
  • <cfset LOCAL.FirstDayOfWeek = (
  • LOCAL.FirstDayOfCalendarYear +
  • (
  • (ARGUMENTS.Week - 1) *
  • 7
  • )) />
  •  
  •  
  • <!---
  • Return the first day of the week for the
  • given year/week combination. Make sure to
  • format the date so that it is not returned
  • as a numeric date (this will just confuse
  • too many people).
  • --->
  • <cfreturn DateFormat( LOCAL.FirstDayOfWeek ) />
  • </cffunction>

Using the above ColdFusion user defined date function, we can get the dates of the first few weeks of 2007:

  • #GetDateByWeek( 2007, 1 )#
  • #GetDateByWeek( 2007, 2 )#
  • #GetDateByWeek( 2007, 3 )#
  • #GetDateByWeek( 2007, 4 )#
  • #GetDateByWeek( 2007, 5 )#
  • #GetDateByWeek( 2007, 6 )#

This gives us the following output:

31-Dec-06
07-Jan-07
14-Jan-07
21-Jan-07
28-Jan-07
04-Feb-07

I hope that helps you out. If I misunderstood what you were trying to do, please let me know.



Reader Comments

Feb 3, 2010 at 12:05 PM // reply »
1 Comments

thank's a lot!


Feb 3, 2010 at 2:22 PM // reply »
10,640 Comments

@Serkan,

No problem.


Feb 18, 2010 at 1:34 PM // reply »
1 Comments

You just saved me a lot of pondering.
Thanks Ben!


Feb 22, 2010 at 8:59 PM // reply »
10,640 Comments

@Ben,

Always glad to help.


Apr 13, 2011 at 12:27 AM // reply »
6 Comments

I'm currently using the week(now()) function to pass the current week to this and it is working great, thanks! My only issue is that I would prefer the start of each week to be based off of Monday instead of Sunday as the week function is using. Any ideas on how I could create a function that would determine the current week based off Monday as the first day of the week?


May 9, 2011 at 12:23 PM // reply »
1 Comments

I'm creating a time sheet app for keeping track of hours in accordance with government requirements. My app already kept track of the week and pay period, this function allowed me to add dates to the days of each week.

THANKS!

Ferrell


Jun 24, 2011 at 3:39 PM // reply »
10 Comments

I just had to add an extra parameter to get the day of month based on the year, week and day of week

Extra parameter:

  • <cfargument
  • name="Day"
  • type="numeric"
  • required="false"
  • hint="The day of week we are looking at (0-6)." />

And just before the <cfreturn>

  • <!---
  • Add the day of week if provided.
  • --->
  • <cfif StructKeyExists( ARGUMENTS, "Day" )>
  • <cfset LOCAL.FirstDayOfWeek = LOCAL.FirstDayOfWeek + ARGUMENTS.Day />
  • </cfif>

That way I'm able to get the exact date of the 5th day of the 26th week of the year 2011!

#GetDateByWeek(2011,26,5)# = 24-Jun-11

Thank you Ben!


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »