Link to home
Start Free TrialLog in
Avatar of RobertoFreemano
RobertoFreemanoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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:
dtReady = dtWantedAt.AddDays(1).Date
to
dtReady = dtWantedAt.AddDays(5).Date

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
 
        nextDOW = dtmDate.AddDays(numDays)
    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)
 
        Dim dtReady As DateTime
 
        '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
            'will be ready on Monday
            dtReady = nextDOW(DayOfWeek.Monday, dtWantedAt).Date
        ElseIf IsAfterHours(dtWantedAt, MinTime, MaxTime) Then
            'will be ready tommorow
            dtReady = dtWantedAt.AddDays(1).Date
        Else
            'will be ready today
            dtReady = dtWantedAt.Date
        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))
                dtReady = dtReady.Add(timeLeftToWait.Add(MinTime))
            Else
                'wanted during hours of operation
                dtReady = dtReady.Add(dtWantedAt.TimeOfDay)
            End If
        Else
            'requested outside of hours of operation
            Console.WriteLine(timeLeftToWait.Hours)
            dtReady = dtReady.Add(MinTime.Add(timeLeftToWait))
        End If
 
        Label1.Text = "Current Time = " & dtNow.ToLongTimeString
        Label2.Text = "Your car will be ready on " & dtReady.ToLongDateString & " at " & dtReady.ToLongTimeString
 
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RobertoFreemano

ASKER

Sorry Chaosian, technical problems with my PC - will test as soon as ;)
Sorry Chaosian,

Will test tonight ;)
Thanks Chaosian ;)
Glad I could help.