Find 'Week Range' for a Date
Get the Code for this Tutorial
View a Working Example of This Code
Comment on this Tutorial
Introduction
This tutorial will show you how to take any given date as input and return the previous Sunday and the next Saturday that the date is between. This is great for anything grouped by date ranges such as reports, invoices, orders, etc.
You can easily modify this example to find a month range for a current date, or any other range that the date falls in. A little more work and you could turn this into a UDF that takes a date, and returns a start and end date for your range.
The Code
First we need a date. This will probably be a date from your database of form, or other dynamic source. We'll use now() for simplicity.
<cfset ourDate = now()>
Next we need to find out how many days until the start of the week. The 1 in this line represents Sunday. If where you live the first day of the week is Monday, change the 1 to a 2. If you needed to start weeks on another day, change the number. Tuesday would be 3, etc..
So starting with our first day of week number, subtract the day of week of the date we're working with. If ourDate was September 12, 2006 dayOfWeek(ourDate) would give us 3 because that day is a Tuesday.
1 minus 3 is -2.
<cfset daysToStartDate = 1 - dayofweek(ourDate)>
Now we need to check if our daysToStartDate value is positive. If it is, it's going to point us to the NEXT weeks start day, so we need to subtract 7 from it.
<cfif daysToStartDate GT 0>
<cfset daysToStartDate = daysToStartDate - 7>
</cfif>
Now let's turn those numbers into an actual date. We know our given date that we are working with, and we know how many days back the first day of the week is. Use dateAdd to make a date string for the first day of our current week.
<cfset startdate = dateadd("d",daysToStartDate,ourDate)>
Pretty easy to get the last day of the week range too, just add 6 to the start date.
<cfset endDate = dateadd("d",6,startdate)>
Conclusion
That's really all there is to it. You can easily make a nice UDF with this too.
____________
Tutorial Code
<!--------------------------------------------
Kevin Sargent
November 12th 2005
http://www.lot-o-nothin.com/cfml/
Please Give Credit Where Used.
--------------------------------------------->
<!--- you can change this to a form value, database value, etc.. to make the script more dynamic --->
<cfset ourDate = now()>
<!--- find out how many days to the start of the week (sunday =1 monday =2, etc..) --->
<cfset daysToStartDate = 1 - dayofweek(ourDate)>
<!--- if daysToStartDate is Positive, we need to subtract 7 days from it to get the previous monday. --->
<cfif daysToStartDate GT 0>
<cfset daysToStartDate = daysToStartDate - 7>
</cfif>
<!--- figure out the date of the previous monday, since we know how many days away it is with daysToStartDate --->
<cfset startdate = dateadd("d",daysToStartDate,ourDate)>
<!--- add 6 days to get to the next sunday --->
<cfset endDate = dateadd("d",6,startdate)>
<cfoutput>
<!--- use dateformat() to output our dates. First the original date--->
<b>Date Entered</b><p>
#dateformat(ourDate,"mm/dd/yyyy")# <br/> #dateformat(ourDate,"dddd")#</p>
<!--- the previous monday --->
<b>From Monday</b>
<p>#dateformat(startdate,"mm/dd/yyyy")# <br/> #dateformat(startdate,"dddd")#</p>
<!--- the ending sunday --->
<b>To Sunday</b>
<p>#dateformat(endDate,"mm/dd/yyyy")# <br/> #dateformat(endDate,"dddd")# </p>
</cfoutput>