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 »
11,246 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 »
11,246 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
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools