Ask Ben: Getting The Date Based On The Year And Week In ColdFusion
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.
Want to use code from this post? Check out the license.
Reader Comments
thank's a lot!
@Serkan,
No problem.
You just saved me a lot of pondering.
Thanks Ben!
@Ben,
Always glad to help.
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?
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
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:
And just before the <cfreturn>
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!
Ben, great function..
@Ferrell.. how did you "add dates to the days of each week"?
I currently have a form & need to add a "Week Selector" drop-down above my days so that when the week is chosen from the drop-down, the correct dates are added to the day fields using Ben's function..
--Choose Week-->Nov 3 thru Nov 9<--
Sun---Mon---Tue---Wed---Thu---Fri---Sat
---------------------------------------
11/03 11/04 11/05 11/06 11/07 11/08 11/09