Hello,
I have make a function (Thanks TIMK From UtterAccess) which make the difference between two dates and which takes only into account a time zone 9H00 - 17H30 and that function perfectly (example if I make the difference between these two hours 8H00 - 20H00, I takes only 9H00 - 17H30). I wanted to improve my function by taking account the midday from 12H00 to 13H30 and if i sum-up, one day is 9h00-12h00// 13H30 - 17H30.
This function run perfectly for one day but not for most days.
Here the examples which pose problems to me:
Date_Start----------------------Date_End---------------Count in second
10/03/03 11:00:00-------11/03/03 10:30:00----------------28800
It must count 1 meal then it counts 2 of them
10/03/03 15:00:00-------12/03/03 16:00:00----------------54000
It must count 2 meal then it counts 3 of them
10/03/03 16:00:00-------13/03/03 17:00:00----------------79200
It must count 3 meal then it counts 4 of them
For me these errors are normal !!
//This part run
Function BetweenHoursInSecond(DateStart As Date, DateEnd As Date) As String
Dim lngSeconds As Long, strHours As String, intHour As Variant, intHour2 As Variant, intHour3 As Variant
' When the DateStart is before 09:00:00
If Format(DateStart, "hh:mm:ss") < "09:00:00" Then
DateStart = DateSerial(Year(DateStart), Month(DateStart), Day(DateStart)) + CDate("09:00:00")
ElseIf Format(DateStart, "hh:mm:ss") > "17:30:00" Then
DateStart = DateSerial(Year(DateStart), Month(DateStart), Day(DateStart)) + CDate("17:30:00")
End If
' When the DateEnd is after 18:00:00
If Format(DateEnd, "hh:mm:ss") > "17:30:00" Then
DateEnd = DateSerial(Year(DateEnd), Month(DateEnd), Day(DateEnd)) + CDate("17:30:00")
ElseIf Format(DateEnd, "hh:mm:ss") < "09:00:00" Then
DateEnd = DateSerial(Year(DateEnd), Month(DateEnd), Day(DateEnd)) + CDate("09:00:00")
End If
intHour = 0
intHour = DateDiff("d", DateStart, DateEnd)
If intHour > 0 Then
lngSeconds = DateDiff("s", DateStart, DateSerial(Year(DateStart), _
Month(DateStart), Day(DateStart)) + CDate("17:30:00")) + _
DateDiff("s", DateSerial(Year(DateEnd), Month(DateEnd), _
Day(DateEnd)) + CDate("09:00:00"), DateEnd)
intHour = ((intHour - 1) * 8.5)
Else
lngSeconds = DateDiff("s", DateStart, DateEnd)
End If
If Len(lngSeconds) > 0 Then
strHours = (intHour * 3600) + lngSeconds
End If
//This part run
intHour2 = 0
intHour3 = 0
intHour2 = DateDiff("d", DateStart, DateEnd)
If intHour2 = 0 Then
If Format(DateStart, "hh:mm:ss") >= "09:00:00" And Format(DateEnd, "hh:mm:ss") <= "12:00:00" Then
intHour3 = 0
ElseIf Format(DateStart, "hh:mm:ss") >= "13:30:00" And Format(DateEnd, "hh:mm:ss") <= "17:30:00" Then
intHour3 = 0
ElseIf Format(DateStart, "hh:mm:ss") >= "09:00:00" And Format(DateEnd, "hh:mm:ss") <= "17:30:00" Then
intHour3 = 5400
End If
//This part doesn't run
ElseIf intHour2 > 0 Then
If Format(DateStart, "hh:mm:ss") >= "09:00:00" And Format(DateEnd, "hh:mm:ss") <= "12:00:00" Then
intHour3 = 0
ElseIf Format(DateStart, "hh:mm:ss") >= "13:30:00" And Format(DateEnd, "hh:mm:ss") <= "17:30:00" Then
intHour3 = 0
ElseIf Format(DateStart, "hh:mm:ss") >= "09:00:00" And Format(DateEnd, "hh:mm:ss") <= "17:30:00" Then
intHour3 = ((DateDiff("d", DateStart, DateEnd)) + 1) * 5400
End If
End If
BetweenHoursInSecond = strHours - intHour3
End Function
Your help will be appraised
Nilses
I have make a function (Thanks TIMK From UtterAccess) which make the difference between two dates and which takes only into account a time zone 9H00 - 17H30 and that function perfectly (example if I make the difference between these two hours 8H00 - 20H00, I takes only 9H00 - 17H30). I wanted to improve my function by taking account the midday from 12H00 to 13H30 and if i sum-up, one day is 9h00-12h00// 13H30 - 17H30.
This function run perfectly for one day but not for most days.
Here the examples which pose problems to me:
Date_Start----------------------Date_End---------------Count in second
10/03/03 11:00:00-------11/03/03 10:30:00----------------28800
It must count 1 meal then it counts 2 of them
10/03/03 15:00:00-------12/03/03 16:00:00----------------54000
It must count 2 meal then it counts 3 of them
10/03/03 16:00:00-------13/03/03 17:00:00----------------79200
It must count 3 meal then it counts 4 of them
For me these errors are normal !!
//This part run
Function BetweenHoursInSecond(DateStart As Date, DateEnd As Date) As String
Dim lngSeconds As Long, strHours As String, intHour As Variant, intHour2 As Variant, intHour3 As Variant
' When the DateStart is before 09:00:00
If Format(DateStart, "hh:mm:ss") < "09:00:00" Then
DateStart = DateSerial(Year(DateStart), Month(DateStart), Day(DateStart)) + CDate("09:00:00")
ElseIf Format(DateStart, "hh:mm:ss") > "17:30:00" Then
DateStart = DateSerial(Year(DateStart), Month(DateStart), Day(DateStart)) + CDate("17:30:00")
End If
' When the DateEnd is after 18:00:00
If Format(DateEnd, "hh:mm:ss") > "17:30:00" Then
DateEnd = DateSerial(Year(DateEnd), Month(DateEnd), Day(DateEnd)) + CDate("17:30:00")
ElseIf Format(DateEnd, "hh:mm:ss") < "09:00:00" Then
DateEnd = DateSerial(Year(DateEnd), Month(DateEnd), Day(DateEnd)) + CDate("09:00:00")
End If
intHour = 0
intHour = DateDiff("d", DateStart, DateEnd)
If intHour > 0 Then
lngSeconds = DateDiff("s", DateStart, DateSerial(Year(DateStart), _
Month(DateStart), Day(DateStart)) + CDate("17:30:00")) + _
DateDiff("s", DateSerial(Year(DateEnd), Month(DateEnd), _
Day(DateEnd)) + CDate("09:00:00"), DateEnd)
intHour = ((intHour - 1) * 8.5)
Else
lngSeconds = DateDiff("s", DateStart, DateEnd)
End If
If Len(lngSeconds) > 0 Then
strHours = (intHour * 3600) + lngSeconds
End If
//This part run
intHour2 = 0
intHour3 = 0
intHour2 = DateDiff("d", DateStart, DateEnd)
If intHour2 = 0 Then
If Format(DateStart, "hh:mm:ss") >= "09:00:00" And Format(DateEnd, "hh:mm:ss") <= "12:00:00" Then
intHour3 = 0
ElseIf Format(DateStart, "hh:mm:ss") >= "13:30:00" And Format(DateEnd, "hh:mm:ss") <= "17:30:00" Then
intHour3 = 0
ElseIf Format(DateStart, "hh:mm:ss") >= "09:00:00" And Format(DateEnd, "hh:mm:ss") <= "17:30:00" Then
intHour3 = 5400
End If
//This part doesn't run
ElseIf intHour2 > 0 Then
If Format(DateStart, "hh:mm:ss") >= "09:00:00" And Format(DateEnd, "hh:mm:ss") <= "12:00:00" Then
intHour3 = 0
ElseIf Format(DateStart, "hh:mm:ss") >= "13:30:00" And Format(DateEnd, "hh:mm:ss") <= "17:30:00" Then
intHour3 = 0
ElseIf Format(DateStart, "hh:mm:ss") >= "09:00:00" And Format(DateEnd, "hh:mm:ss") <= "17:30:00" Then
intHour3 = ((DateDiff("d", DateStart, DateEnd)) + 1) * 5400
End If
End If
BetweenHoursInSecond = strHours - intHour3
End Function
Your help will be appraised
Nilses