以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  如何计算两个时间差,只算上班时间,以小时为单位。  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=72686)

--  作者:able88
--  发布时间:2015/8/4 16:30:00
--  如何计算两个时间差,只算上班时间,以小时为单位。

知道OA中表单的收件时间和审批时间,如何计算收件时间和审单时间的差,(以小时为单位),只算上班

时间,休息时间不算在内。
上班时间为:
星期一到星期六
上午8:30到12:00
下午1:30到6:00


--  作者:大红袍
--  发布时间: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编辑过]

--  作者:able88
--  发布时间:2015/8/5 13:41:00
--  中文解释说明

没看懂,能不能对代码加一些必要的中文解释说明


--  作者:大红袍
--  发布时间: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

 


--  作者:wumingrong1
--  发布时间:2018/7/16 22:09:00
--