ASKER
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 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 _
dtWantedAt.TimeOfDay > MaxTime) Or _
(dtWantedAt.DayOfWeek = DayOfWeek.Saturday Or _
dtWantedAt.DayOfWeek = DayOfWeek.Sunday) Then
'will be ready on Monday
dtReady = nextDOW(DayOfWeek.Monday, dtWantedAt).Date
ElseIf (dtWantedAt.TimeOfDay > 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 ((dtNow.TimeOfDay > MinTime) And (dtNow.TimeOfDay < MaxTime)) Then
'requested during hours of operation
If (dtWantedAt.TimeOfDay > MaxTime) Or (dtWantedAt.TimeOfDay < MinTime) 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
ASKER
ASKER
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 Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MaxTime As New TimeSpan(22, 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 _
(dtWantedAt.Hour > MaxTime.Hours) Or _
dtWantedAt.DayOfWeek = DayOfWeek.Saturday Or _
dtWantedAt.DayOfWeek = DayOfWeek.Sunday) Then
'will be ready on Monday
dtReady = nextDOW(DayOfWeek.Monday, dtWantedAt).Date
ElseIf (dtWantedAt.Hour > MaxTime.Hours) 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 ((dtNow.Hour > MinTime.Hours) And (dtNow.Hour < MaxTime.Hours)) Then
'requested during hours of operation
If (dtWantedAt.Hour > MaxTime.Hours) Or (dtWantedAt.Hour < MinTime.Minutes) 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
Public Class WorkTimer
Private Const START_OF_DAY As Integer = 8
Private Const END_OF_DAY As Integer = 17
Public Shared Function GetFinishTime(ByVal startTime As DateTime, ByVal spanToAdd As TimeSpan) As DateTime
startTime = adjustStartForWeekend(startTime)
If spanToAdd.Days >= 1 Then
'You can deal with weekends if you have time spans > 1 day to handle. Model it similarly to the hours.")
Throw New ArgumentOutOfRangeException("Can only add < 24 hours with this function.")
End If
Dim endTime As DateTime
endTime = addWorkTime(endTime, spanToAdd)
endTime = addWeekendDays(endTime)
Return endTime
End Function
Private Shared Function adjustStartForWeekend(ByVal startTime As DateTime) As DateTime
Select Case startTime.DayOfWeek
Case DayOfWeek.Saturday
Return startTime.Date.AddDays(2).AddHours(START_OF_DAY)
Case DayOfWeek.Sunday
Return startTime.Date.AddDays(1).AddHours(START_OF_DAY)
Case Else
Return startTime
End Select
End Function
Private Shared Function addWeekendDays(ByVal endTime As DateTime) As DateTime
Select Case endTime.DayOfWeek
Case DayOfWeek.Saturday
Return endTime.AddDays(2)
Case DayOfWeek.Sunday
Return endTime.AddDays(1)
Case Else
Return endTime.AddDays(0)
End Select
End Function
Private Shared Function addWorkTime(ByVal startTime As DateTime, ByVal span As TimeSpan) As DateTime
Dim now As DateTime = DateTime.Now
Dim endOfToday As DateTime = DateTime.Today.AddHours(END_OF_DAY)
Dim remainingTimeToday As TimeSpan = endOfToday - now
If remainingTimeToday > span Then
Return now.Add(span)
Else
span = span.Subtract(remainingTimeToday)
Return now.Date.AddDays(1).AddHours(START_OF_DAY).Add(span)
End If
End Function
End Class
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
ASKER
ASKER
rivate 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
Public Class WorkTimer
Private Const START_OF_DAY As Integer = 8
Private Const END_OF_DAY As Integer = 17
Public Shared Function GetFinishTime(ByVal startTime As DateTime, ByVal spanToAdd As TimeSpan) As DateTime
startTime = adjustStartForWeekend(startTime)
If spanToAdd.Days >= 1 Then
'You can deal with weekends if you have time spans > 1 day to handle. Model it similarly to the hours.")
Throw New ArgumentOutOfRangeException("Can only add < 24 hours with this function.")
End If
Dim endTime As DateTime
endTime = addWorkTime(endTime, spanToAdd)
endTime = addWeekendDays(endTime)
Return endTime
End Function
Private Shared Function adjustStartForWeekend(ByVal startTime As DateTime) As DateTime
Select Case startTime.DayOfWeek
Case DayOfWeek.Saturday
Return startTime.Date.AddDays(2).AddHours(START_OF_DAY)
Case DayOfWeek.Sunday
Return startTime.Date.AddDays(1).AddHours(START_OF_DAY)
Case Else
Return startTime
End Select
End Function
Private Shared Function addWeekendDays(ByVal endTime As DateTime) As DateTime
Select Case endTime.DayOfWeek
Case DayOfWeek.Saturday
Return endTime.AddDays(2)
Case DayOfWeek.Sunday
Return endTime.AddDays(1)
Case Else
Return endTime.AddDays(0)
End Select
End Function
Private Shared Function addWorkTime(ByVal startTime As DateTime, ByVal span As TimeSpan) As DateTime
Dim now As DateTime = DateTime.Now
Dim endOfToday As DateTime = DateTime.Today.AddHours(END_OF_DAY)
Dim remainingTimeToday As TimeSpan = endOfToday.Subtract(now)
If remainingTimeToday.CompareTo(span) > 0 Then
Return now.Add(span)
Else
span = span.Subtract(remainingTimeToday)
Return now.Date.AddDays(1).AddHours(START_OF_DAY).Add(span)
End If
End Function
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
ASKER
ASKER
ASKER
Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.
TRUSTED BY
ASKER
Dim dt2 As Date = Data.Now.AddHours(8)
--------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Current Time = " & dt1
Label2.Text = "Your car will be ready on Mondayy at... = " & dt2
End Sub