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

Posted May 22, 2007 at 1:04 PM

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:

 Launch code in new window » Download code as text file »

  • <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:

 Launch code in new window » Download code as text file »

  • #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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

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

thank's a lot!


Feb 3, 2010 at 2:22 PM // reply »
7,572 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 »
7,572 Comments

@Ben,

Always glad to help.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »