|
| |
|
|
CraigBFG
Posts: 211 Joined: 7/25/2003 From: UK Status: offline
|
Calculate date with working days - 8/18/2003 10:58:10
I need the facility to calculate a date based on the number of working days. I'm guessing this takes into account weekends only, as public holidays can change from year to year. Example. Date query raised : Friday 1st Sept. Priority Assigned : 3 days (working days that is) Due Date : Tues 5th Sept. The Priority is different depending on the type of work required. I know this isn't a simple operation, and I have seen something similar around 't find it now. Thanks
|
|
|
|
rdouglass
Posts: 9280 From: Biddeford, ME USA Status: offline
|
RE: Calculate date with working days - 8/18/2003 11:55:38
I have a function that I use quite often: <% FUNCTION calcEndDate(startDate,numberOfDays) tempDate = startDate IF (weekday(tempDate,1) = 7) THEN tempDate = dateAdd("d", 2, tempDate) END IF IF (weekday(tempDate,1) = 1) THEN tempDate = dateAdd("d", 1, tempDate) END IF FOR x = 1 to(numberOfDays) tempDate = dateAdd(dateAddVar, 1, tempDate) IF (weekday(tempDate,1) = 1) OR (weekday(tempDate,1) = 7) THEN x = x - 1 END IF NEXT calcEndDate = tempDate END FUNCTION %> You'd use it similar to this: <%=calcEndDate(Request("StartDate"),Request("TotalDays"))%> You'd use that lone if you were posting from a form that had those 2 form fields on it. You can replace the "Request"s with any variable or constant you want (DB values, querystrings, etc.). I also have a version I use that does include holidays drawn from a DB if you're interested, however it is a little more complicated.... Hope it helps.... EDIT: FYI the first two IF...THEN's check to see if the "startDate" is a weekend day and adjusts accordingly.....
< Message edited by rdouglass -- 8/18/2003 11:57:45 AM >
_____________________________
Don't take you're eye off your final destination. ASP Checkbox Function Tutorial.
|
|
|
|
Alex_C
Posts: 2 Joined: 9/8/2003 Status: offline
|
RE: Calculate date with working days - 9/8/2003 6:42:42
Hi, being on a locked-down workstation without the appropriate add-ins, this came in very useful. So much so that I thought I'd feed back into the community an extended version able to handle negative offsets, i.e. number of working days BEFORE a given date, rather than just later. Anyone may use it - but please acknowledge copyright, as I have. Code follows: Function OffsetWorkingDay(StartDate, numberOfDays) 'original code (c) rdouglass http://www.frontpagewebmaster.com/m_151408/tm.htm 'extended code (c) A Croll 07/09/03 - handles nil/negative offsets 'new code 'Enumeration value String Unit of time interval to add '------------------------------------------------------------------------- 'DateInterval.Day d Day; truncated to integral value 'DateInterval.DayOfYear y Day; truncated to integral value 'DateInterval.Hour h Hour; rounded to nearest millisecond 'DateInterval.Minute n Minute; rounded to nearest millisecond 'DateInterval.Month m Month; truncated to integral value 'DateInterval.Quarter q Quarter; truncated to integral value 'DateInterval.Second s Second; rounded to nearest millisecond 'DateInterval.Weekday w Day; truncated to integral value 'DateInterval.WeekOfYear ww Week; truncated to integral value 'DateInterval.Year yyyy Year; truncated to integral value' dateAddVar = "d" 'new code ends tempDate = StartDate 'new code If Not numberOfDays = 0 Then 'new code ends If (Weekday(tempDate, 1) = 7) Then 'is a Saturday 'old code superseded 'tempDate = DateAdd("d", 2, tempDate) 'new code If numberOfDays > 0 Then tempDate = DateAdd("d", 2, tempDate) numberOfDays = numberOfDays - 1 Else If numberOfDays < 0 Then tempDate = DateAdd("d", -1, tempDate) numberOfDays = numberOfDays + 1 End If End If 'new code ends End If If (Weekday(tempDate, 1) = 1) Then 'is a Sunday 'old code superseded 'tempDate = DateAdd("d", 1, tempDate) 'new code If numberOfDays > 0 Then tempDate = DateAdd("d", 1, tempDate) numberOfDays = numberOfDays - 1 Else If numberOfDays < 0 Then tempDate = DateAdd("d", -2, tempDate) numberOfDays = numberOfDays + 1 End If End If 'new code ends End If ' new code If numberOfDays >= 0 Then ' original code For x = 1 To (numberOfDays) tempDate = DateAdd(dateAddVar, 1, tempDate) If (Weekday(tempDate, 1) = 1) Or (Weekday(tempDate, 1) = 7) Then x = x - 1 End If Next ' new code Else For x = 1 To (-numberOfDays) tempDate = DateAdd(dateAddVar, -1, tempDate) If (Weekday(tempDate, 1) = 1) Or (Weekday(tempDate, 1) = 7) Then x = x - 1 End If Next End If ' new code ends 'new code End If 'NOT numberOfDays = 0 'new code ends OffsetWorkingDay = tempDate End Function 'OffsetWorkingDay(StartDate, numberOfDays)
|
|
|
|
CraigBFG
Posts: 211 Joined: 7/25/2003 From: UK Status: offline
|
RE: Calculate date with working days - 9/18/2003 11:01:21
Hi Do you have a script to work out the number of working days between 2 dates? Thanks Craig
|
|
|
|
Alex_C
Posts: 2 Joined: 9/8/2003 Status: offline
|
RE: Calculate date with working days - 9/18/2003 11:32:31
Sorry, I don't, although it wouldn't be too difficult to work out. Actually, now I come to think about it, I've seen code for it - on the Microsoft website, I think.
|
|
|
|
CraigBFG
Posts: 211 Joined: 7/25/2003 From: UK Status: offline
|
RE: Calculate date with working days - 9/18/2003 11:33:42
I too have seen a function on the MS website but its for use in Access and I don't know how to make this work as an asp script function.
|
|
|
|
interweb
Posts: 4 Joined: 2/1/2006 Status: offline
|
RE: Calculate date with working days - 2/1/2006 20:17:43
Sorry to dredge up an old post (found through Google) but I am having a bear of a time trying to adapt this function to take into account UPS holidays. I want to use this as a UPS estimated arrival calculator based on their holiday schedule. Just to make it more fun, I also need to set the "start date" based on whether it is after 3 PM on a weekday or not. If its prior to 3 PM (weekday, non-holiday) the clock starts from the current date. If its after 3 PM, clock starts on the next business day (weekday non-holiday). I have a couple bits already that may be helpful. Array of UPS holidays HolidayArray = array("12/31/2005","1/1/2006","05/29/2006","07/04/2006","09/04/2006","11/23/2006","12/25/2006","12/31/2006","1/01/2007") AND a function that can determine if a date is a holiday: Function isHoliday(aDate,HolidayArray) isHoliday = False For Each HolidayItem in HolidayArray If CDate(HolidayItem) = CDate(aDate) Then isHoliday = True Exit For End If Next End Function Any ideas out there?
|
|
|
|
rdouglass
Posts: 9280 From: Biddeford, ME USA Status: offline
|
RE: Calculate date with working days - 2/2/2006 8:57:22
quote:
If its prior to 3 PM (weekday, non-holiday) the clock starts from the current date. If its after 3 PM, clock starts on the next business day (weekday non-holiday). Hi and Welcome to OutFront Is this the hold up? If so, I suspect you should be able to use DateAdd somehow. I envision something like: ... isHoliday = False IF DatePart("h",Now()) > 14 THEN adate = DateAdd("d",1,adate) END IF For Each HolidayItem in HolidayArray If CDate(HolidayItem) = CDate(aDate) Then ... At least that's how I'd approach it.
_____________________________
Don't take you're eye off your final destination. ASP Checkbox Function Tutorial.
|
|
|
|
interweb
Posts: 4 Joined: 2/1/2006 Status: offline
|
RE: Calculate date with working days - 2/2/2006 12:41:51
Thanks for the response. I have the logic for checking time of day. The problem I'm having is inconsistent results using the Function provided by Alex_C. Most weeks without holidays work fine. There are a couple holiday weekends that don't calculate correctly namely Memorial Day & Thanksgiving. I'd post the code I have but it is hopelessly messed up. Essentially I took Alex_C's function, stripped out the negative offset forks and inserted the isHoliday function into the "For x = 1 To (numberOfDays) " loop. I may end up having to build something using UPS's "Time in Transit" tool but would really like to avoid third party network communication during the process.
|
|
|
|
interweb
Posts: 4 Joined: 2/1/2006 Status: offline
|
RE: Calculate date with working days - 2/3/2006 19:21:07
Finally worked it all out. Thought I might share what I ended up with. This calculates Estimated UPS Delivery date and takes into account UPS holidays and weekends. Its purpose is to show delivery date for Next Day, Second Day, and Saturday delivery (when appropriate) as well as taking into account time of day (merchant ships same day for non-holiday weekday orders before 3 PM, otherwise it ships next business day). The way I'm using it, the result is inserted into an existing option list of shipping options. Credit to those who went before me, however this is basically a rewrite to accommodate the features I needed. Note that UPS Holidays are stored in a database table named "upsHolidays" containing one column named "holidayDate" datatype: datetime, no nulls, primary key. They are stored in short format i.e., mm/dd/yyyy.
<%
''' code is designed to be in an include file
Dim CurrentDate, CurrentTime, showSaturday
Dim StartDate, tempStartDate, tempDeliveryDate, tempSaturdayDate, DayCounter
Dim upsHolidaySQL, upsHolidayConn, upsHolidayRS, HolidayArray, HolidayItem
HolidayArray = upsHolidays
CurrentDate = Date()
CurrentTime = Time()
StartDate = getStartDate(CurrentDate,CurrentTime)
showSaturday = showSaturdayShipOption(CurrentDate,CurrentTime)
Function upsHolidays
upsHolidaySQL = "SELECT holidayDate FROM upsHolidays Order by holidayDate"
Set upsHolidayConn = Server.CreateObject("ADODB.Connection")
upsHolidayConn.open Session("DSN_Name")
Set upsHolidayRS = upsHolidayConn.Execute(upsHolidaySQL)
upsHolidays = upsHolidayRS.GetRows
upsHolidayRS.Close
Set upsHolidayRS = Nothing
upsHolidayConn.Close
Set upsHolidayConn = Nothing
End Function
Function getStartDate(CurrentDate,CurrentTime)
tempStartDate = CurrentDate
If WeekDay(tempStartDate) = 7 Then
tempStartDate = DateAdd("D", 2, tempStartDate)
ElseIf WeekDay(tempStartDate) = 1 Then
tempStartDate = DateAdd("D", 1, tempStartDate)
Else
If isHoliday(tempStartDate,HolidayArray) Then
Do Until isHoliday(tempStartDate,HolidayArray) = False
tempStartDate = DateAdd("D", 1, tempStartDate)
Loop
Else
If FormatDateTime(CurrentTime,4) > FormatDateTime("2:59:59 PM",4) Then
tempStartDate = DateAdd("D", 1, tempStartDate)
Do Until isHoliday(tempStartDate,HolidayArray) = False AND NOT (WeekDay(tempStartDate) = 7 OR WeekDay(tempStartDate) = 1)
tempStartDate = DateAdd("D", 1, tempStartDate)
Loop
End if
End if
End if
If isHoliday(tempStartDate,HolidayArray) Then
Do Until isHoliday(tempStartDate,HolidayArray) = False AND NOT (WeekDay(tempStartDate) = 7 OR WeekDay(tempStartDate) = 1)
tempStartDate = DateAdd("D", 1, tempStartDate)
Loop
End if
getStartDate = tempStartDate
End Function
Function getDeliveryByDate(StartDate, numberOfDays)
tempDeliveryDate = DateAdd("D", 1, StartDate)
DayCounter = 0
Do Until DayCounter = numberOfDays
If isHoliday(tempDeliveryDate,HolidayArray) = False AND NOT (WeekDay(tempDeliveryDate) = 7 OR WeekDay(tempDeliveryDate) = 1) Then
DayCounter = DayCounter + 1
End if
If DayCounter = numberOfDays Then
getDeliveryByDate = " Delivery by " & WeekdayName(WeekDay(tempDeliveryDate)) & " " & tempDeliveryDate
Exit Do
Else
tempDeliveryDate = DateAdd("D", 1, tempDeliveryDate)
End if
Loop
End Function
Function isHoliday(aDate,HolidayArray)
isHoliday = False
For HolidayItem = LBound(HolidayArray,2) To UBound(HolidayArray,2)
If CDate(HolidayArray(0,HolidayItem)) = CDate(aDate) Then
isHoliday = True
Exit For
End If
Next
End Function
Function showSaturdayShipOption(CurrentDate,CurrentTime)
If WeekDay(CurrentDate) = 5 OR WeekDay(CurrentDate) = 6 Then
Select Case WeekDay(CurrentDate)
Case "5" ' Thursday
If FormatDateTime(CurrentTime,4) > FormatDateTime("2:59:59 PM",4) Then
showSaturdayShipOption = getSaturdayDate(CurrentDate)
Else
showSaturdayShipOption = False
End if
Case "6" ' Friday
If isHoliday(CurrentDate,HolidayArray) = True Then
showSaturdayShipOption = False
Else
If FormatDateTime(CurrentTime,4) < FormatDateTime("3:00:01 PM",4) Then
showSaturdayShipOption = getSaturdayDate(CurrentDate)
Else
showSaturdayShipOption = False
End if
End if
End Select
Else
showSaturdayShipOption = False
End if
End Function
Function getSaturdayDate(CurrentDate)
tempSaturdayDate = CurrentDate
Do Until WeekDay(tempSaturdayDate) = 7
tempSaturdayDate = DateAdd("D", 1, tempSaturdayDate)
If isHoliday(tempSaturdayDate,HolidayArray) = True Then
getSaturdayDate = False
Exit Do
End if
Loop
getSaturdayDate = "Delivery by " & WeekdayName(WeekDay(tempSaturdayDate)) & " " & tempSaturdayDate
End Function
%> Function is then called like this: <%=getDeliveryByDate(StartDate,1)%> where the numeral is the number of days e.g., next day = 1 & 2nd day = 2.
< Message edited by interweb -- 2/4/2006 11:52:56 >
|
|
|
|
interweb
Posts: 4 Joined: 2/1/2006 Status: offline
|
RE: Calculate date with working days - 2/4/2006 11:51:45
Realized that I didn't really give a good example of use for this code. Here's an example that better illustrates how its used. The code above minus this block: interwebCurrentDate = Date()
interwebCurrentTime = Time()
interwebStartDate = getStartDate(interwebCurrentDate,interwebCurrentTime)
showSaturday = showSaturdayShipOption(interwebCurrentDate,interwebCurrentTime) is in the file "shippingExpectedDate.asp". The code below is the page that displays the results.
<!--#include file="shippingExpectedDate.asp"-->
<%
interwebCurrentDate = Date()
interwebCurrentTime = Time()
interwebStartDate = getStartDate(interwebCurrentDate,interwebCurrentTime)
showSaturday = showSaturdayShipOption(interwebCurrentDate,interwebCurrentTime)
%>
<table>
<form>
<tr>
<td>Shipping Option</td>
<td>
<select name="Shipping" size="1">
<option value="2">UPS Next Business Day - <%=getDeliveryByDate(interwebStartDate,1)%> $24.50</option>
<option value="3">UPS Second Business Day - <%=getDeliveryByDate(interwebStartDate,2)%> $14.50</option>
<% If showSaturday <> False Then %>
<option value="4">UPS Saturday Delivery - <%=showSaturday%> $44.50</option>
<% End if %>
<option value="1">Express Mail 1-2 Business Days - $19.50</option>
<option value="0">Free Shipping 3-5 Business Days - $0.00</option>
</select>
</td>
</tr>
</form>
</table>
|
|
New Messages |
No New Messages |
Hot Topic w/ New Messages |
Hot Topic w/o New Messages |
Locked w/ New Messages |
Locked w/o New Messages |
|
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts
|
|
|