[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

next business day function?

Posted on 2004-08-05
15
Medium Priority
?
234 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

831 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