汗...想复杂了,如下。
mark 工作时间的计算
Dim wtimes() As String = {"8:30-12:00", "13:30-18:00"}
Dim stime As Date = "2015-08-01 07:30"
Dim etime As Date = "2015-08-03 19:00"
Dim sumHours As Double = 0
Dim sp As TimeSpan
Dim dhours As Double = 0
If stime.DayOfWeek <> 0 Then
For Each wtime As String In wtimes
Dim ary() As String = wtime.Split("-")
Dim d1 As Date = stime.Date & " " & ary(0)
Dim d2 As Date = stime.Date & " " & ary(1)
sp = d2 - d1
dhours += sp.TotalHours
If stime < d1 Then
If etime > d2 Then
sp = d2 - d1
sumhours += sp.TotalHours
ElseIf etime > d1 Then
sp = etime - d1
sumhours += sp.TotalHours
End If
ElseIf stime < d2 Then
If etime > d2 Then
sp = d2 - stime
sumhours += sp.TotalHours
ElseIf etime > d1 Then
sp = etime - stime
sumhours += sp.TotalHours
End If
End If
Next
End If
Dim d As Date = stime.AddDays(1)
Do While d.Date < etime.Date
If d.DayOfWeek <> 0 Then
sumhours += dhours
End If
d = d.AddDays(1)
Loop
If etime.DayOfWeek <> 0 AndAlso stime.Date < etime.Date Then
For Each wtime As String In wtimes
Dim ary() As String = wtime.Split("-")
Dim d1 As Date = etime.Date & " " & ary(0)
Dim d2 As Date = etime.Date & " " & ary(1)
If etime > d2 Then
sp = d2 - d1
sumhours += sp.TotalHours
ElseIf etime > d1 Then
sp = etime - d1
sumhours += sp.TotalHours
End If
Next
End If
msgbox("两日期相隔:" & sumhours & "小时")
[此贴子已经被作者于2015/8/4 18:06:28编辑过]