Ask Ben: Getting the Previous Day In ColdFusion, Excluding Saturday And Sunday

Posted July 22, 2007 at 1:32 PM by Ben Nadel

Tags: ColdFusion, Ask Ben

The question is too long to list here, but basically someone contacted me looking for help with a file review system. The part that was causing issue was the fact that he needed to review the files from the previous day. In order figure out the previous day Tuesday-Friday, it was fine. When it comes to weekend, changing of the month, or changing of the year, he was doing a whole lot of crazy date alterations.

No problem - we've all been there. Until you realize how much Date/Time functionality ColdFusion has built-in, you usually do take it upon yourself to figure out all the logic required to handle things like weekends and year cross-overs. Thankfully, ColdFusion plays very nicely with date math, and when date math is not enough, ColdFusion's DateAdd() function can really pick up the slack. In addition to that, ColdFusion also has Year(), Month(), and Day() functions for parsing out parts of the date/time stamp.

Long story-short, getting the previous day (excluding weekends) is as easy as calling ColdFusion's DateAdd() function with the "w" date part. The "w" date part stands for Weekday. For a long time, I didn't understand the difference between Day and Weekday in the context of DateAdd(). They do basically the same thing, most of the time. The difference, which can be subtle depending on what date you are looking at, is that adding or subtracting Weekdays to and from a date will hop over the weekends. Therefore, if you add a weekday to Friday, it will return the next Monday. Similarly, if you subtract a Weekday from a Monday, it will return the previous Friday.

This being said, getting the previous weekday is quite a simple task. To demonstrate, we will get the previous 10 weekdays (excluding Saturday and Sunday):

  • <!---
  • Get the current DATE. Fix()'ing the date will chop
  • off the time portion of the date/time stamp.
  • Caution: This will result in a NUMERIC date, not
  • a standard date.
  • --->
  • <cfset dtNow = Fix( Now() ) />
  •  
  • <!---
  • Now, let's loop over the past 10 WEEKDAYS. This is
  • different from days. Weekday math does not include
  • weekends in its calculations.
  • --->
  • <cfloop
  • index="intOffset"
  • from="0"
  • to="10"
  • step="1">
  •  
  • <!--- Get the WEEKDAY that we want to show. --->
  • <cfset dtDay = DateAdd( "w", -intOffset, dtNow ) />
  •  
  • <!---
  • Output the full date so we can see BOTH the day
  • of the week and the day-date.
  • --->
  • #DateFormat( dtDay, "full" )#<br />
  •  
  • </cfloop>

Running the above code, we get the following output:

Monday, July 23, 2007
Friday, July 20, 2007
Thursday, July 19, 2007
Wednesday, July 18, 2007
Tuesday, July 17, 2007
Monday, July 16, 2007
Friday, July 13, 2007
Thursday, July 12, 2007
Wednesday, July 11, 2007
Tuesday, July 10, 2007
Monday, July 9, 2007

Notice that neither Saturday nor Sunday are showing up in this list. ColdFusion makes life easy.




Reader Comments

Jul 23, 2007 at 8:34 AM // reply »
67 Comments

Hi Ben.
I originally saw this question here: http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=1&catid=3&threadid=1285943&enterthread=y

Note my - purposely abrupt - response to it. Thankfully the OP realised it was for emphasis rather than anything else.

So this is my moment for "I've learned something today". I had no idea that dateAdd() had a "weekday" option.

It would be great if you could post something on the Adobe forums pointing to your post here (or just replicate the answer), lest anyone accidentally stumble across the issue and decide either Frank's or my suggested solution is the best way forward.

Cheers.

--
Adam


Jul 23, 2007 at 8:41 AM // reply »
11,238 Comments

@Adam,

I have posted a link on the Adobe Forums. It's funny, I never use the Adobe forums, and this is like the second or third time that I have been asked the same exact question as one on the forums. At least, this time it was the same person who contacted me (Frank) as the person who posted on the forums.

For the longest time I knew that WeekDay was there as an option, but I could not for the life of me figure out what HECK was the difference between a Day and Weekday???? I mean what other kind of day is there? A MonthDay??? It wasn't until I was working with an event calendar that I realized what the Weekday actually did.


Jul 23, 2007 at 10:20 AM // reply »
67 Comments

If you wanted to start helping out on the Adobe forums - even occasionally - that would be cool: we need all the help we can get!

--
Adam


Jul 23, 2007 at 4:02 PM // reply »
11 Comments

Hi Ben,

Not as simple as your solution. However, I use the similar function in SQL. I dig it up after I read your blog.

http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/

Good post Ben!

Pinal Dave


Jul 23, 2007 at 4:59 PM // reply »
11,238 Comments

Nice SQL UDF. I don't use enough SQL UDFs. They are really handy and can make the queries seem so much nicer.


Mar 13, 2008 at 7:09 PM // reply »
1 Comments

With Coldfusion 8 is the weekday option "w" now stand for weeks?


Mar 17, 2008 at 7:34 AM // reply »
11,238 Comments

@Chris,

According to the documentation, "w" still stands for weekday.


Jun 24, 2008 at 3:47 PM // reply »
1 Comments

I was using your example to deal with a scheduling app I'm writing, and notice that your output returns 11 dates - the date you started with plus the next 10.

Changing the "from" in the loop to "1" skips the date you start with and provides the next 10. Is there something I can change in the looping to give me 10 including the start date - i.e. the date I start with plus 9?

Thanks!


Jun 25, 2008 at 7:38 AM // reply »
11,238 Comments

@Susan,

Just go from 0 to 9, rather than 0 to 10.


Aug 21, 2008 at 8:32 AM // reply »
1 Comments

This was almost exactly what I needed, thank you for posting this simple solution!

I needed to provide a select box for the next 7 days (including weekend days) so users can select what future day they desire. I modified the code in order to parse the day (string) to appear on the page and the date as the value (to insert in the db).

Here's my code if anyone is interested:

<cfoutput>
<cfset dtNow = Fix( Now() ) />

<cfloop
index="intOffset"
from="0"
to="6"
step="1">

<cfset dtDay = DateAdd( "d", intOffset, dtNow ) />

#dateformat(dtDay,"m/dd/yyyy")# #DayofWeekAsString(DayOfWeek(dtDay))#<br />

</cfloop>
</cfoutput>


PJ
Sep 27, 2011 at 8:22 PM // reply »
5 Comments

Hi Ben,

Sadly, CF8's "datediff" function has w mean the same as ww -- it only counts weeks.

So I'm trying to find the difference between two dates which are, say, a Thursday and a Tuesday, and the result it gives me is 0, because it's not a complete week.

Seems pointless to have w option in datediff at all, if it's the same as weeks (one might as well use weeks, and you lose the awesome functionality of 'weekdays').

Now, date math using just a number is one thing, that seems to work with the weekdays, but comparing two dates seems another, you need datediff for that.

I found this (old) blogpost while googling for an answer...

PJ


Post A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
May 20, 2013 at 10:45 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Ben - I believe you can achieve the same functionality with ColdFusion's built in ArrayToList() function. ArrayToList( users[ "id" ] ); ... read »
May 20, 2013 at 10:21 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Is there any error logging and handling framework in angularjs, if not then in what way I can do this. ... read »
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools