Ask Ben: Getting The Date Based On The Year And Week In ColdFusion
Posted May 22, 2007 at 1:04 PM
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
Newer Post
Ben Forta On The ColdFusion 8 Scorpio Tour
Older Post
When Do You Violate The Law of Demeter In ColdFusion OOP Development?
Reader Comments
thank's a lot!
@Serkan,
No problem.
You just saved me a lot of pondering.
Thanks Ben!
@Ben,
Always glad to help.




