Let me clarify a bit. What I mean is let's say you need to know the beginning of the current week and lets also say that the beginning of your week is always Sunday.

There are many ways to do this, and this way will allow you all kinds of flexibility (not that other ways won't). I will go over a few other ways I found today over at Rays blog post. I wrote this some 9 years ago for a time application I wrote. No matter what date you choose in a calendar widget, I needed to determine that weeks Sunday date. So this is how I accomplished that. My original code also calculated all kinds of other variables that related to the given task at hand but this was one of them.

view plain print about
1<cfset obj = structNew()>
2<!--- lets setup a date here and call it td (temp date) --->
3<cfset obj.td = dateAdd('d',-4,now())>
4<!--- get the ordinal day of the week for that date --->
5<cfset obj.ToDay = DayOfWeek(obj.td)>
6<!--- if the ordinal day of that week is a 1 or a Sunday, lets get last weeks beginning --->
7<cfif obj.ToDay EQ 1>
8
9 <cfset obj.lastsunday = DATEADD('d',-7,obj.td)>
10 <cfset obj.thissunday = obj.td>
11<!--- if the ordinal day of that week is a Monday, lets get the previous Sunday and the current from yesterday --->
12<cfelseif obj.ToDay EQ 2>
13 <cfset obj.lastsunday = DATEADD('d',-8,obj.td)>
14 <cfset obj.thissunday = DATEADD('d',-1,obj.td)>
15<cfelse>
16 <!--- else tues(3) - sat(6), lets do some math and figure it out --->
17 <cfset obj.tmp = obj.ToDay - 1>
18 <cfset obj.lastsunday = DATEADD('d',-(obj.tmp+7),obj.td)>
19 <cfset obj.thissunday = DATEADD('d',-obj.tmp,obj.td)>
20</cfif>
21
22<cfdump var="#obj#">

From the dump we can see that we now end up with a struct that has last sunday and this sunday. this will change based on the starting date (td).

Play around if you want, here is the Demo

Now over at Ray's blog mentioned earlier, there are far more easier ways to get the same result.

Gert shows this approach:

view plain print about
1// s
view plain print about
etup the1 url string, <parscfsete t he dcSguin day str=u ct int(tonow (p)ull/ o7ut )all *pa7st +hos1t >
2n(a4m0e
9923)i
f3(cogr
i.4serve<r_scfsetoftw are dNEQS unda"Ayp ac=h e"no w(AND len() cgi.-q uery_now(stri)n g))% 7 + 1>
5(40993.663{5764)

Which works very very well, both are math based and each solve the problem.

Jason offered another approach:

view plain print about
1dtThisWeek = fix( *userdate* )
2objThisWeek = structNew()
3objThisWeek.Start = dateFormat(dtThisWeek - dayOfWeek( dtThisWeek ) + 1)
4objThisWeek.End = dateFormat( objThisWeek.Start + 6 )

What is very interesting is that the FIX() function produces the same result as Gert's variation - 1. so

view plain print about
1<cfset dSunday = int(now()/7)*7+1>
is the same as
view plain print about
1<cfset dSunday = fix(now())+1>
Very cool. I love learning new ways to do things.

Jason's is very similar in nature to my approach but much more condensed. He is using the FIX() cf function to convert a date to an integer then subtracting the ordinal date and adding 1 to find the beginning of the week. Again very nice approach with a small code footprint.

I am sure there are a number of other ways this can be done. To bad there is no built in function to hand out this value. Maybe it will be in a future release.

Anyway, I would love to see more variations on this.