Ask Ben: Getting The Start And End Dates Of The Previous Week In ColdFusion

Posted November 3, 2008 at 9:06 AM

Tags: ColdFusion, Ask Ben

Hi Ben, Please how do I get beginning of previous week to the end of previous week. I'm working on something that needs last week's date to run today. Thank you.

I do now and will always love working with dates in ColdFusion. The fact that you can seamlessly switch from standard dates to numeric dates and do all kind of fast math on them makes them just a pleasure to work with. Not to mention the fact that date math is generally faster that using methods like DateDiff() and DateAdd(). Anyway, getting to the question above, let's not get distracted by the fact that we are dealing with the "previous" week. What we need to think of is the simple fact that we need to get the start and end dates of week given a date within it. Once we see that, then, as I have demonstrated before, this is simply a matter of a little math:

 Launch code in new window » Download code as text file »

  • <!---
  • Get today's date; but then subtract 7 days from it so that
  • we have "this" day of last week. The Fix() method simply
  • removes the time stamp (and converts date to numeric date).
  • --->
  • <cfset dtLastWeek = (Fix( Now() ) - 7) />
  •  
  • <!---
  • Now that we have a day in last week, we can easily grab the
  • start and end of the week.
  • --->
  • <cfset objLastWeek = StructNew() />
  •  
  • <!--- Get start of week. --->
  • <cfset objLastWeek.Start = DateFormat(
  • dtLastWeek - DayOfWeek( dtLastWeek ) + 1
  • ) />
  •  
  • <!--- Get end of week by adding to start date. --->
  • <cfset objLastWeek.End = DateFormat( objLastWeek.Start + 6 ) />
  •  
  •  
  • <!--- Output the week delimiters. --->
  • <cfoutput>
  • <p>
  • Start: #objLastWeek.Start#<br />
  • End: #objLastWeek.End#
  • </p>
  • </cfoutput>

The reason that we have to use DateFormat() for the dates when calculating them is that a byproduct of date math is the fact that it turns standard dates into numeric dates; these are date/time stamps represented as floating point values where the integer is the day and the fraction is the time. You can certainly leave the dates in numeric format, but I find that this can confuse people (and will fail calls to IsDate()). By passing the values through the DateFormat() as an intermediary handler, it converts the numeric dates back into standard dates.

Running the above code, we get:

Start: 26-Oct-08
End: 01-Nov-08

I hope that helps.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

May 20, 2009 at 10:12 AM // reply »
2 Comments

Is it possible to display all the days in the current week beginning on a Monday. For example:

Monday 5/18/2009
Tuesday 5/19/2009
Wednesday 5/20/2009
Thursday 5/21/2009
Friday 5/22/2009
Saturday 5/23/2009
Sunday 5/24/2009


May 20, 2009 at 1:01 PM // reply »
7,572 Comments

@Jay,

No problem - all you would have to do is get the Monday of the given week, then use a CFLoop to for 7 days.

For more information check out these links:

http://www.bennadel.com/blog/136-Using-CFLoop-To-Loop-Over-ColdFusion-Dates.htm

http://www.bennadel.com/blog/893-Date-Looping-Using-ColdFusion-Custom-DateLoop-Tag.htm


Mar 9, 2010 at 3:36 PM // reply »
1 Comments

Is this example providing the previous week or is it providing a span of 7 days which may include last week?


Mar 19, 2010 at 9:15 AM // reply »
7,572 Comments

@David,

It gets the last week. I goes back 7 days, and then finds the start / end of that week.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »