Solved

next business day function?

Posted on 2004-08-05
15
226 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:theamzngq
  • 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 500 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:theamzngq
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 35

Expert Comment

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

Author Comment

by:theamzngq
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 500 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:theamzngq
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:theamzngq
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:theamzngq
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:theamzngq
ID: 11737899
shouldn't it be

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

?
0
 
LVL 2

Author Comment

by:theamzngq
ID: 11737900
too many periods....hehe

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

Author Comment

by:theamzngq
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to redirect https://www to non-www? 6 76
URL redirect 4 67
Intranet Solution - Sharepoint Foundation or up 4 67
Web Site Hosting 10 89
This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

792 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