Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help - Horrible Date Formula!!!

Status
Not open for further replies.

fragglemoo

IS-IT--Management
Nov 20, 2002
41
0
0
GB
I'm using Crystal 2008 and have been given this VB formula to do some time calculations. I need to get it to work in CR, can anyone help me? I'm totally lost :(

Thanks.

Function WorkingHours(startDateTime As Variant, endDateTime As Variant)


Const FIVE_THIRTY_PM = 17.5 / 24#


Const NINE_AM = 9# / 24#



Dim startDate As Variant, startTime As Variant, endDate As Variant, endTime As Variant, _

totalTime As Variant, days As Integer

If Trim$(startDateTime) = "" Or Trim$(endDateTime) = "" Then

WorkingHours = ""

Exit Function


End If


startDate = CDate(Split(startDateTime, " ")(0))


startTime = Split(startDateTime, " ")(1)


startTime = (CInt(Split(startTime, ":")(0)) + CInt(Split(startTime, ":")(1)) / 60) / 24


endDate = CDate(Split(endDateTime, " ")(0))


endTime = Split(endDateTime, " ")(1)


endTime = (CInt(Split(endTime, ":")(0)) + CInt(Split(endTime, ":")(1)) / 60) / 24


totalTime = 0#


If startTime < NINE_AM Then


startTime = NINE_AM


End If


If endTime > FIVE_THIRTY_PM Then


endDate = NextWorkingDay(endDate)


endTime = NINE_AM + endTime - FIVE_THIRTY_PM


End If


If startDate = endDate Then

WorkingHours = (endTime - startTime)

Exit Function

End If


If startTime <= FIVE_THIRTY_PM Then
totalTime = FIVE_THIRTY_PM - startTime

End If


startDate = NextWorkingDay(startDate)


totalTime = totalTime + endTime - NINE_AM

days = 0

Do While startDate < endDate

startDate = startDate + 1

If Weekday(startDate, vbMonday) <= 5 Then

days = days + 1


End If


Loop 'Until startDate >= endDate


WorkingHours = totalTime + days * 8.5 / 24

End Function


Private Function NextWorkingDay(current As Variant) As Date


Dim dOW As Integer



current = current + 1


dOW = Weekday(current, vbMonday)

If dOW > 5 Then

current = current + 8 - dOW

End If
NextWorkingDay = current

End Function

 
Please show sample raw data and desired results. It could be that you do not need this formula at all, but without knowing what your desired output is, no one can say.

Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports

"What version of URGENT!!! are you using?
 
Thanks for the reply.

I've got 2 date/time fields

Date entered and fix date. I need to be able to calculate the total fix hours with this support hours range. Mon - Fri 9-5:30.

Thanks again.
 
Please show sample data and desired results. Also please define "fix" hours as opposed to hours.

Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports

"What version of URGENT!!! are you using?
 
Date Entered 05/03/08 09:00
Fix Date 06/03/08 09:30

I need to calculate the fix hours i.e the number of hours between the 2 date within the business hours Mon - Fri 9-5:30. for the example above I am expecting a total of 9 hrs

Does that make sense?
 
Not really. The actual hours between the 2 date/times is 744.5 (31 days * 24 hours, plus the additional 1/2 hour).

If you only want working hours M-F then it would be considerably less, but a lot more than 9. So please explain your logic.

Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports

"What version of URGENT!!! are you using?
 
I'm a bit confused now.

5/03/08 09:00 between 9-5:30 = 8.5 hours
6/03/08 09:30 between 9-5:30 = .5 hours

 
Date Entered 05/03/08 09:00
Fix Date 06/03/08 09:30

That is 31 days and 1/2 hour of elapsed time.



Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports

"What version of URGENT!!! are you using?
 
Ahhh I'm in the UK so our date format is dd/mm/yy.

Does it make more sense now?
 
Are date entered and fix date 2 separate records, or 2 fields of the same record?

Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports

"What version of URGENT!!! are you using?
 
There are 2 sep records (diff fields from diff tables).
 
Panic over! we've managed to do it.

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top