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:
- hint="Gets the first day of the week of the given year/week combo.">
- <!--- Define arguments. --->
- hint="The year we are looking at."
- 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(
- ) />
- 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 ) +
- ) />
- 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) *
- )) />
- 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 ) />
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:
I hope that helps you out. If I misunderstood what you were trying to do, please let me know.
Looking For A New Job?
- Senior ColdFusion / Mura Developer at Fig Leaf Software
- ColdFusion Developer - Backend Systems at Intuvo LLC
- Front End Developer - Web/Mobile Developer - Event App company at MeetingPlay
- Web Developer and More! (Network Engineer/System Admin/Web Developer) at United Clinical Laboratories
thank's a lot!
You just saved me a lot of pondering.
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.
I just had to add an extra parameter to get the day of month based on the year, week and day of week
- 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 />
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<--
11/03 11/04 11/05 11/06 11/07 11/08 11/09