Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

next business day function?

Posted on 2004-08-05
15
Medium Priority
?
233 Views
Last Modified: 2013-12-24
Is there a "next business day" function when working with dates, or do I have to do it the 'hard' way?
0
Comment
Question by:David Williamson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 35

Expert Comment

by:mrichmon
ID: 11730952
There is not a "nextbusinessday" function, but there is the following which caluclates the number of business days inbetween two days and part of the code could be easily used to test if the current day was a business day or not.

<!--- CF_WorkDateDiff
Syntax: <CF_WorkDateDiff StartDate="StartDate" EndDate="EndDate" ClosedDays="List of dates closed" ReturnVariable="somevarname">
OR
<cfmodule template="CF_WorkDateDiff.cfm" StartDate="StartDate" EndDate="EndDate" ClosedDays="List of dates closed" ReturnVariable="somevarname">

This tag takes a start and end date and calculates the number of working days in between - not counting closed dates.
The result, the number of working days as an integer, is returned in the variable specified by ReturnVariable,
in the calling template's scope. --->

<!--- Tag Parameters --->
<cfparam name="ATTRIBUTES.StartDate" type="date">
<cfparam name="ATTRIBUTES.EndDate" type="date">
<cfparam name="ATTRIBUTES.ClosedDays" type="string" default="">
<cfparam name="ATTRIBUTES.ReturnVariable" type="variablename">

<cfset num_closed = 0><!--- Count closed days that fall in range --->

<!--- Count number of closed days in date range --->
<cfloop index="dayoff" list="#ATTRIBUTES.ClosedDays#">
      <cfif (DateCompare(ATTRIBUTES.StartDate, dayoff) EQ -1) AND (DateCompare(dayoff, Attributes.EndDate) LE 0)>
            <cfset num_closed = num_closed + 1>
      </cfif>
</cfloop>
<cfset start_day = DayOfWeek(ATTRIBUTES.StartDate)>
<cfset end_day = DayOfWeek(ATTRIBUTES.EndDate)>
<cfset date_diff_in_days = DateDiff("d", ATTRIBUTES.StartDate, ATTRIBUTES.EndDate)>
<cfset whole_weeks = date_diff_in_days\7>
<cfset days_left = date_diff_in_days mod 7>

<!-- Count weekends in whole weeks --->
<cfset non_bus_days = whole_weeks * 2>

<!--- Count partial or whole weekends for partial weeks --->
<cfif days_left>
      <cfif end_day EQ 7 OR start_day EQ 7>
            <cfset non_bus_days = non_bus_days + 1>
      <cfelseif start_day GT end_day>
            <cfset non_bus_days = non_bus_days + 2>
      </cfif>
</cfif>

<!--- total business days --->
<cfset bus_days = date_diff_in_days - non_bus_days - num_closed>
<!--- Retunr result to calling template --->
<cfset "Caller.#ATTRIBUTES.ReturnVariable#" = bus_days>
0
 
LVL 35

Assisted Solution

by:mrichmon
mrichmon earned 2000 total points
ID: 11730966
The other option is that there is a DayOfWeek function.  So you could use logic like

Take currentdate
Loop
     Add one day
     If new date is not (sunday or saturday) and day is not in a list of closed days then break
End Loop
You now have the next business day.
 
0
 
LVL 2

Author Comment

by:David Williamson
ID: 11730978
the latter is what I was going to end up doing.  I really like your custom tag, though.  I'll have to see if I come up with a need to use it.
0
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 
LVL 35

Expert Comment

by:mrichmon
ID: 11731546
You could easily write that into a tag....
0
 
LVL 2

Author Comment

by:David Williamson
ID: 11731639
I have never made a custom tag before.  It is something I have been planning to look up.  Perhaps now is the time!
0
 
LVL 35

Accepted Solution

by:
mrichmon earned 2000 total points
ID: 11731888
Very easy.

Use the above as an example, just change teh calculation.

You define a
<cfparam name="ATTRIBUTES.StartDate" type="date">
for each incoming parameter (in this case the date and maybe a list of closed dates)

Then you do the calcualtion like a normal cf page

Then you return the result like this :
<!--- Retunr result to calling template --->
<cfset "Caller.#ATTRIBUTES.ReturnVariable#" = next_day>

Then simply place the file in the CustomTags directory and call like a normal cf tag

<CF_filename StartDate="#var#" ClosedDates="#var2#" ReturnVariable="nextbusinessday">

Then in the page access
#nextbusinessday#

variable that was set by tag.
0
 
LVL 2

Author Comment

by:David Williamson
ID: 11737078
here is my new custom tag:

<cfparam name="attributes.StartDate" type="date">
<cfparam name="attributes.NoOfDays" type="numeric">

<cfset today = attributes.StartDate>
<cfoutput>
<cfset noofbusdays = 0>
<cfloop condition="NoOfBusDays LT attributes.NoOfDays">
      <cfset today = today + CreateTimeSpan(1,0,0,0)>
      <cfif NOT ListFind('1,7',dayofweek(today))>
            <cfset noofbusdays = noofbusdays + 1>noofbusdays - #noofbusdays#<br>
            <font color="##00FF00">Today #dateformat(today)# is #dayofweekasstring(dayofweek(today))#</font><br>
      <cfelse>
            Today #dateformat(today)# is #dayofweekasstring(dayofweek(today))#<br>
      </cfif>
</cfloop>
</cfoutput>
<cfset "Caller.#ATTRIBUTES.ReturnVariable#" = businessDay>


When I run it I get an error: Element RETURNVARIABLE is undefined in ATTRIBUTES.

Any ideas?

0
 
LVL 2

Author Comment

by:David Williamson
ID: 11737228
I fixed the error by doing some more reading on custom tags.  The last line in the tag now reads:
<cfset Caller.businessDay = dateformat(today)>

Thanks for your help!  I might have taken a while to get around to writing some custom tags without this thread.  My father, a programmer from way back, was advising me to start to modularize my code in such a way that I could re-use things (like writing custom tags) to minimize bugs and troubleshooting.  I see the advantages and wisdom now...
0
 
LVL 2

Author Comment

by:David Williamson
ID: 11737307
with out my display stuff in it:

<cfparam name="attributes.StartDate" type="date">
<cfparam name="attributes.NoOfDays" type="numeric">

<cfset today = attributes.StartDate>
<cfoutput>
<cfset noofbusdays = 0>
<cfloop condition="NoOfBusDays LT attributes.NoOfDays">
      <cfset today = today + CreateTimeSpan(1,0,0,0)>
      <cfif NOT ListFind('1,7',dayofweek(today))>
            <cfset noofbusdays = noofbusdays + 1>
      </cfif>
</cfloop>
</cfoutput>
<cfset Caller.businessDay = dateformat(today)>
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 11737849
Sorry - just got the email messages saying you updated this post....

The problem with the way you fixed it is that you are forcing the user to define a variable called businessDay in the page that will be calling this tag.

The better way to fix it is to add the follwowing code (1 line inserted below indicated in comments) which allows the user to name the variable whatever they want :

================================================

<cfparam name="attributes.StartDate" type="date">
<cfparam name="attributes.NoOfDays" type="numeric">

<!--- Add the following line --->
<cfparam name="ATTRIBUTES.ReturnVariable" type="variablename">

<cfset today = attributes.StartDate>
<cfoutput>
<cfset noofbusdays = 0>
<cfloop condition="NoOfBusDays LT attributes.NoOfDays">
     <cfset today = today + CreateTimeSpan(1,0,0,0)>
     <cfif NOT ListFind('1,7',dayofweek(today))>
          <cfset noofbusdays = noofbusdays + 1>
     </cfif>
</cfloop>
</cfoutput>
<cfset Caller.businessDay = dateformat(today)>

================================================

Then it would be called as

<CF_filename StartDate="#startday#" NoOfDays="1" returnvariable="businessday">

(assuming you had <cfset Startday = "3/01/2004"> above in the page)
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 11737888
Oh and the last line would be changed back to :
<cfset "Caller.#ATTRIBUTES.ReturnVariable#" = businessDay>
0
 
LVL 2

Author Comment

by:David Williamson
ID: 11737899
shouldn't it be

<cfset Caller..#ATTRIBUTES.ReturnVariable# = dateformat(today)>

?
0
 
LVL 2

Author Comment

by:David Williamson
ID: 11737900
too many periods....hehe

<cfset Caller.#ATTRIBUTES.ReturnVariable# = dateformat(today)>
0
 
LVL 2

Author Comment

by:David Williamson
ID: 11737932
This is my final answer:

<cfset "Caller.#ATTRIBUTES.ReturnVariable#" = dateformat(today)>
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 11738006
:o)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question