在工作日结束时间的最佳方法是什么？What is the best way to wrap time around the work day?

``````Private Function GetDateRequired() As Date
''// A decimal representation of the current hour
Dim hours As Decimal = Decimal.Parse(Date.Now.Hour) + (Decimal.Parse(Date.Now.Minute) / 60.0)

Dim delay As Decimal = 3.0           ''// delay in hours
Dim endOfDay As Decimal = 12.0 + 5.0 ''// end of day, in hours
Dim startOfDay As Decimal = 8.0      ''// start of day, in hours

Dim newHour As Integer
Dim newMinute As Integer

Dim dateRequired As Date = Now
Dim delta As Decimal = hours + delay

''// Wrap around to the next day, if necessary
If delta > endOfDay Then
delta = delta - endOfDay

newHour = Integer.Parse(Decimal.Truncate(delta))
newMinute = Integer.Parse(Decimal.Truncate((delta - newHour) * 60))
newHour = startOfDay + newHour
Else
newHour = Integer.Parse(Decimal.Truncate(delta))
newMinute = Integer.Parse(Decimal.Truncate((delta - newHour) * 60))
End If

dateRequired = New Date(dateRequired.Year, dateRequired.Month, dateRequired.Day, newHour, newMinute, 0)

Return dateRequired
End Sub
``````

3 个回复3

===============>>#1 票数：3

``````Module Module1

Public Function IsInBusinessHours(ByVal d As Date) As Boolean
Return Not (d.Hour < 8 OrElse d.Hour > 17 OrElse d.DayOfWeek = DayOfWeek.Saturday OrElse d.DayOfWeek = DayOfWeek.Sunday)
End Function

Public Function AddInBusinessHours(ByVal fromDate As Date, ByVal hours As Integer) As Date
Dim work As Date = fromDate.AddHours(hours)
End While
Return work
End Function

Public Function LoopInBusinessHours(ByVal fromDate As Date, ByVal hours As Integer) As Date
Dim work As Date = fromDate
While hours > 0
While hours > 0 AndAlso IsInBusinessHours(work)
hours -= 1
End While
End While
End While
Return work
End Function

Sub Main()
Dim test As Date = New Date(2008, 8, 8, 15, 0, 0)
Dim hours As Integer = 5
Console.WriteLine("Date: " + test.ToString() + ", " + hours.ToString())
End Sub

End Module
``````

===============>>#3 票数：1

``````now <- number of minutes since the work day started
delay <- number of minutes in the delay
day <- length of a work day in minutes

x <- (now + delay) / day {integer division}
y <- (now + delay) % day {modulo remainder}

return startoftoday + x {in days} + y {in minutes}
``````

4回复

2回复

3回复

4回复

3回复

3回复

1回复

4回复

2回复

1回复