Foxtable(狐表)用户栏目专家坐堂 → 如何计算两个时间差,只算上班时间,以小时为单位。


  共有3004人关注过本帖树形打印复制链接

主题:如何计算两个时间差,只算上班时间,以小时为单位。

帅哥哟,离线,有人找我吗?
大红袍
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/8/4 18:04:00 [显示全部帖子]

汗...想复杂了,如下。

 

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编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
大红袍
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/8/5 14:17:00 [显示全部帖子]

自己改这一段

 

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"

 

没看懂,先去看一下视频教程

 

http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&Id=67187

 


 回到顶部