您好,我是VB net中日期时间的新手,我正在尝试计算生产中某些操作的时间。例如:
starTtime="01.12.2018 07:00"
endTime ="01.12.2018 15:00"
我有两个不工作的例子:
Pause1Start =“ 01.12.2018 10:00” Pause1Stop =“ 01.12.2018 10:30”
Pause2Start =“ 01.12.2018 14:00” Pause2Stop =“ 01.12.2018 14:30”
如何计算已用时间? 并非总是这个示例,必须检查两个日期时间之间是否包含暂停时间,或者是否有工作日,周末等。 因此,操作可以在一天之内开始,并在一天后结束。
我需要知道如何使用它吗?
谢谢
答案 0 :(得分:0)
尝试此代码。我建议您定义暂停开始和结束的列表,然后循环浏览它们,检查是否在工作时间内出现了特定的暂停,并将该时间(以秒为单位)添加到保持总秒数的变量中:
Sub Main()
Dim startTime = New DateTime(2018, 11, 1, 7, 0, 0)
Dim endTime = New DateTime(2018, 11, 1, 17, 0, 0)
Dim pauseStarts = New List(Of DateTime)
pauseStarts.Add(New DateTime(2018, 11, 1, 16, 0, 0))
pauseStarts.Add(New DateTime(2018, 11, 1, 10, 0, 0))
Dim pauseEnds = New List(Of DateTime)
' Add respective pause endings
pauseEnds.Add(New DateTime(2018, 11, 1, 18, 0, 0))
pauseEnds.Add(New DateTime(2018, 11, 1, 11, 30, 0))
Dim pauseSeconds As Long = 0
For i = 0 To pauseStarts.Count - 1
If pauseStarts(i) < endTime AndAlso pauseEnds(i) > startTime Then
' get the minimum from two times
Dim starting = If(startTime > pauseStarts(i), startTime, pauseStarts(i))
' get maximum from two times
Dim ending = If(endTime < pauseEnds(i), endTime, pauseEnds(i))
' add seconds during pause
pauseSeconds += (ending - starting).TotalSeconds
End If
Next
End Sub