# calculate Time Due From a DateTime value in business hours (cont) - vb.net 2003

Hi Experts,

I have code which provides a time estimation based on 8 hours (between 08:00 - 19:00) Mon-Fri. ignoring sat/sun.

I wish to amend the code to provide a time estimation based on 5 working days.

The code below works for 8hours... I have tried to change the code myself (but I'm a bit thick).

I've tried changing:
to

But I can't seem to figure out how to do this. I tied the number 40 (based on 8x5) but this still didn't work.

Thanks,
Roberto

``````Code provided by MijaeDjinn.

Public Class Form1
Function nextDOW(ByVal dow As DayOfWeek, ByVal dtmDate As Date) As Date
' Find the next specified day of the week
' after the specified date.
Dim numDays As Integer
'sunday = 0
If dtmDate.DayOfWeek > dow Then
numDays = 7 - (dtmDate.DayOfWeek - dow)
Else
numDays = dow - dtmDate.DayOfWeek
End If

End Function

Private Function IsAfterHours(ByVal dt As DateTime, ByVal min As TimeSpan, ByVal max As TimeSpan) As Boolean
If dt.Hour > max.Hours Then Return True
If (dt.Hour = max.Hours) And (dt.Minute > max.Minutes) Then Return True
If (dt.Hour < min.Hours) Then Return True
If (dt.Hour = min.Hours) And (dt.Minute < min.Minutes) Then Return True
Return False
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MaxTime As New TimeSpan(18, 0, 0)
Dim MinTime As New TimeSpan(7, 59, 0)
'Dim timeRemaining As TimeSpan

Dim dtNow As DateTime = Now
Dim dtWantedAt As DateTime = dtNow.AddHours(8)

'calculate what day it will be ready
If (dtWantedAt.DayOfWeek = DayOfWeek.Friday And _
IsAfterHours(dtWantedAt, MinTime, MaxTime)) Or _
(dtWantedAt.DayOfWeek = DayOfWeek.Saturday Or _
dtWantedAt.DayOfWeek = DayOfWeek.Sunday) Then
ElseIf IsAfterHours(dtWantedAt, MinTime, MaxTime) Then
Else
End If

'calculate what time it will be ready
Dim timeLeftToWait As New TimeSpan(8, 0, 0)

If Not IsAfterHours(dtNow, MinTime, MaxTime) Then
'requested during hours of operation
If IsAfterHours(dtWantedAt, MinTime, MaxTime) Then
'wanted outside of hours of operation
timeLeftToWait = timeLeftToWait.Subtract(MaxTime.Subtract(dtNow.TimeOfDay))
Else
'wanted during hours of operation
End If
Else
'requested outside of hours of operation
Console.WriteLine(timeLeftToWait.Hours)
End If

Label1.Text = "Current Time = " & dtNow.ToLongTimeString

End Sub
End Class
``````
###### Who is Participating?

Commented:
Here's my take on a solution. It's a little more flexible -- you can give it any number of hours for your estimate.

I wasn't sure what your rules were around end-of-day issues, so there may be some invalid assumptions there. There's also duplicated code in here that could be pulled out into another method, but that's cleanup work.
``````Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dtNow As DateTime = Now
Dim calculator As New TimeCalculator
Dim dtReady As DateTime = calculator.CalculateTime(dtNow, 40)
Label1.Text = "Current Time = " & dtNow.ToLongTimeString
End Sub
End Class

Public Class TimeCalculator
''' <summary>
''' goal is to add the number of hours to the current time
''' assuming work hours are Mon-Fri, 0800-1900
''' </summary>
''' <param name="currentTime"></param>
''' <param name="numberOfHours"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function CalculateTime(ByVal currentTime As DateTime, ByVal numberOfHours As Integer) As DateTime
Dim readyTime As DateTime = currentTime

Select Case currentTime.DayOfWeek
Case DayOfWeek.Saturday, DayOfWeek.Sunday
Case Else
' this can be made cleaner, but we'll assume that the estimate isn't being made exactly on the hour
Dim remainingHoursToday As Integer = 18 - currentTime.Hour
If remainingHoursToday > 0 Then
With currentTime
readyTime = New DateTime(.Year, .Month, .Day, 8, 0, 0)
End With

' adjust to skip the weekend
Case DayOfWeek.Saturday
Case DayOfWeek.Sunday
End Select

numberOfHours -= remainingHoursToday
End If
End Select

While numberOfHours > 0
Case DayOfWeek.Saturday, DayOfWeek.Sunday
Case Else
If numberOfHours > 11 Then
numberOfHours -= 11
Else
numberOfHours = 0
End If
End Select
End While

End Function
End Class
``````
0

Author Commented:
Sorry Chaosian, technical problems with my PC - will test as soon as ;)
0

Author Commented:
Sorry Chaosian,

Will test tonight ;)
0

Author Commented:
Thanks Chaosian ;)
0

Commented: