VB.net 获取两个时间跨度之间的总时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16639675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:38:57  来源:igfitidea点击:

VB.net Get total time between two time spans

vb.nettimespan

提问by Malcolm Salvador

I was wondering if there is a function with VB.NET that can tell me the total time that two different time spans have in common.

我想知道 VB.NET 是否有一个函数可以告诉我两个不同时间跨度共有的总时间。

for example, I have these two different time spans:

例如,我有这两个不同的时间跨度:

1st Date : (2013-01-01 8:30 AM) - (2013-01-01 11:30PM)
2nd Date : (2013-01-01 10:00 PM) - (2013-01-02 6:00 AM)

Can VB.NET tell me that the answer is 1 hour and 30 mins? (perhaps in decimal) It is 1 hour and 30 minutes because up to 10:00PM in the evening and 11:30 in the evening falls in with the other time span.

VB.NET 能告诉我答案是 1 小时 30 分钟吗?(可能是十进制)是 1 小时 30 分钟,因为晚上 10:00 PM 和晚上 11:30 属于其他时间跨度。

Summary:

概括:

I just need a function that will tell me how many hours in a certain timespan is a part of the night shift. (that's 10:00 PM to 6:00 AM).

我只需要一个函数来告诉我在特定时间跨度内有多少小时是夜班的一部分。(即晚上 10:00 到早上 6:00)。

采纳答案by thunderbird

Date1 and Date2 represents the first time span and Date2 and Date3 represent the second time span. If the order of the dates are changed you will have to define an analogous else to the first if.

Date1 和Date2 代表第一个时间跨度,Date2 和Date3 代表第二个时间跨度。如果日期的顺序改变了,你将不得不定义一个类似于第一个 if 的 else。

    Dim date1 As Date = #1/1/2013 8:30:00 AM#
    Dim date2 As Date = #1/1/2013 11:30:00 PM#
    Dim date3 As Date = #1/1/2013 10:00:00 PM#
    Dim date4 As Date = #1/2/2013 6:00:00 AM#
    Dim timePeriod As TimeSpan

    If date3.Date >= date1.Date And date3.Date <= date2.Date Then
        If date4.Date <= date2.Date Then
            timePeriod = date4.TimeOfDay - date3.TimeOfDay
        Else
            timePeriod = date2.TimeOfDay - date3.TimeOfDay
        End If
    End If
    Msgbox(timePeriod.ToString)

EDIt: Just timePeriod = date4- date3will convert the difference into timespan

编辑:只是timePeriod = date4- date3将差异转换为时间跨度

EDIT 2:The code above would have returned date2-date3 even if there there was no common time span. The code below would return 0 in such a case. Also when comparing two Datevariables in an Ifstatement, the TimeOfDaypart is neglected.

编辑 2:即使没有共同的时间跨度,上面的代码也会返回 date2-date3。在这种情况下,下面的代码将返回 0。同样在比较语句中的两个Date变量时If,该TimeOfDay部分被忽略。

        Dim date1 As Date = #1/1/2013 8:30:00 AM#
        Dim date2 As Date = #1/1/2013 11:30:00 PM#
        Dim date3 As Date = #1/1/2013 10:00:00 PM#
        Dim date4 As Date = #1/2/2013 6:00:00 AM#
        Dim timePeriod As TimeSpan
        If date3 >= date1 And date3 <= date2 Then
            If date4 <= date2 Then
                timePeriod = date4 - date3                  
            ElseIf date3 < date2 Then                   
                timePeriod = date2 - date3
            ElseIf date3.Date = date2.Date Then                    
                If date3.TimeOfDay < date2.TimeOfDay Then                        
                    timePeriod = date2 - date3
                Else
                timePeriod = TimeSpan.FromDays(0)
                End If
            Else
                timePeriod = TimeSpan.FromDays(0)
            End If
        End If
        MsgBox(timePeriod.ToString)

回答by Will

This functionality is available to all .Net languages via the TimeSpanclass:

此功能可通过TimeSpan类用于所有 .Net 语言:

eg.:

例如。:

result = (dt2 - dt1).TotalHours

When you subtract two dates you get a TimeSpan. The TotalHours property of the TimeSpan gives you the number of hours, or part thereof.

当你减去两个日期时,你会得到一个时间跨度。TimeSpan 的 TotalHours 属性为您提供小时数或其中的一部分。

Check out the docs: http://msdn.microsoft.com/en-us/library/269ew577(v=vs.110).aspx

查看文档:http: //msdn.microsoft.com/en-us/library/269ew577(v= vs.110).aspx

回答by Douglas Barbin

EDIT: Sorry, I misunderstood your question. I think this is what you are looking for.

编辑:对不起,我误解了你的问题。我想这就是你要找的。

Here is your DateRange class:

这是您的 DateRange 类:

Public Class DateRange
   Public Property StartTime As DateTime
   Public Property EndTime As DateTime
   Public ReadOnly Property NumberOfHours As Decimal
       Get
           Dim result As Double
           result += (EndTime - StartTime).Hours
           result += (EndTime - StartTime).Minutes / 60
           result += (EndTime - StartTime).Seconds / 3600
           Return result
       End Get
   End Property
End Class

This would be the C# function from the link, rewritten in VB:

这将是链接中的 C# 函数,用 VB 重写:

Private Function GetIntersectionRange(range1 As DateRange, range2 As DateRange) As DateRange
    Dim iRange As New DateRange()
    iRange.StartTime = If(range1.StartTime < range2.StartTime, range2.StartTime, range1.StartTime)
    iRange.EndTime = If(range1.EndTime < range2.EndTime, range1.EndTime, range2.EndTime)

    If iRange.StartTime > iRange.EndTime Then iRange = Nothing
    Return iRange
End Function

Here is a little snippet of code to do the calculation you asked and write the answer to the console:

这是一小段代码,用于执行您要求的计算并将答案写入控制台:

Dim firstDate As New DateRange With {.StartTime = New DateTime(2013, 1, 1, 8, 30, 0), .EndTime = New DateTime(2013, 1, 1, 23, 30, 0)}
Dim secondDate As New DateRange With {.StartTime = New DateTime(2013, 1, 1, 22, 0, 0), .EndTime = New DateTime(2013, 1, 2, 6, 0, 0)}

Dim result As DateRange = GetIntersectionRange(firstDate, secondDate)
Console.WriteLine(result.NumberOfHours)

回答by Douglas Barbin

Like this?

像这样?

            Dim d1 As DateTime = "1/22/2018 8:30:00 AM"
            Dim d2 As DateTime = "1/29/2018 5:30:00 PM"
            Dim ts As TimeSpan = d2.Subtract(d1)
            MessageBox.Show("Total Hours are " & Convert.ToDouble(Math.Round(ts.TotalHours, 2)))

Just play around with it

随便玩玩