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

Posted November 3, 2008 at 9:06 AM by Ben Nadel

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:

• <!---
• 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.

### Looking For a New Job?

25% of job board revenue is donated to Kiva. Loans that change lives - Find out more »

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

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 »

@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.

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

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 »

@David,

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

May 17, 2012 at 1:36 PM // reply »

Ben

This was exactly what I was looking for, but didn't know exactly how to approach it. You've saved me a bunch of time! Cheers

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

 Author Name: Author Email: Author Website: Comment: Supported HTML tags for formatting: bold   italic   code Remember my information Subscribe to comments Send me a copy of this comment
InVision App - Prototyping Made Beautiful With Prototyping Tools Recent Blog Comments
Mar 8, 2014 at 5:21 PM
hi Ben, how can we mention our own custom tags instead of using HTML tags in coldfusion? kindly revert with an example. ... read »
Mar 8, 2014 at 1:28 PM
Experimenting With Multiple Class Inheritance In Javascript
Hi, Agree with Artavazd, it's not inheritance. The proof : console.log(ben instanceof Person) // false console.log(ben instanceof Monkey) // false console.log(ben instanceof Ben) // true If " ... read »
Mar 7, 2014 at 8:31 PM
Sanity Check: \$index vs. DOM In AngularJS Directives
I had NOOOO idea you could pass the entire friend object into a method like this: removeFriend( friend ) . I was always using some ID of the object and passing that around back and forth between vie ... read »
Mar 7, 2014 at 10:43 AM
Project HUGE: Active Release Technique (ART) With Dr. Christopher Anselmi In NYC
Does anyone know of a GOOD A.R.T. Provider near the Binghamton,NY area? I have gone to a couple of people that said they do ART but they don't. It's just a massage. Thanks for any help you can give ... read »
Mar 7, 2014 at 8:44 AM
GMail Seems To Ignore The Return-Path Header Defined By The CFMail FailTo Attribute
So, is the header "Problems-to" no longer valid? I also add "return-path" via cfmailparam. ... read »
Mar 6, 2014 at 8:58 PM
Posting XML With ColdFusion, CFHttp, And CFHttpParam
ERROR2: Missing type node :( ... read »
Mar 6, 2014 at 6:56 AM
CFLoop Attributes Evaluated Only Once
Hi Ben, An "oldie but a goodie"... funny enough we just had a discussion about this at work, what with JavaScript evaluating at each iteration of a for loop it stems to reason that CF woul ... read »
Mar 5, 2014 at 6:27 AM
Defining Instantiatable Classes In The AngularJS Dependency Injection Framework