【问题标题】:VB.net Get total time between two time spansVB.net 获取两个时间跨度之间的总时间
【发布时间】:2013-05-19 21:32:29
【问题描述】:

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

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

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)

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

总结:

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

【问题讨论】:

  • this 你在找什么吗?
  • 谢谢 Neolisk,会调查的。是否有与此等效的 VB?
  • 我尝试将他的 C# 语法翻译成 VB 代码,但我想我在这方面做得很差。DATERANGE 和 DATERANGEVENTARGS 一样吗?
  • @Malky.Kid 第一次约会或第二次约会可以跨越两天还是仅仅一天?
  • 好吧 date2 确实跨越了两天。

标签: vb.net timespan


【解决方案1】:

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

例如:

result = (dt2 - dt1).TotalHours

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

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

【讨论】:

  • 谢谢威尔的回答。
  • @DouglasBarbin:区间的交集是一个比这更复杂的问题。您需要考虑以下情况,1) 当您的时间间隔不相交时(A 在 B 之前,B 在 A 之前),2) 有一个共同的间隔,或者一个包含另一个。您最终可能会使用TotalHours,但这本身并不是解决方案。
  • 对。我应该更好地表达它。这将替换 DateRange 类,但您仍需要其他函数。
  • 啊,我一定是看错了问题,或者它已被编辑。无论如何,我的回答是错误的,所以请忽略。感谢道格拉斯的信任投票! =)
【解决方案2】:

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)

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

编辑 2:即使没有共同的时间跨度,上面的代码也会返回 date2-date3。在这种情况下,下面的代码将返回 0。此外,当比较 If 语句中的两个 Date 变量时,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)

【讨论】:

  • 先生,您应该喝一杯冰镇啤酒。非常感谢。
  • @Malky.Kid 嘿,我修复了代码中的错误。请参阅编辑 2。
【解决方案3】:

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

这是你的 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

这将是链接中的 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

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

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)

【讨论】:

  • 对不起。我误解了。现在检查我的答案。
  • 我明白了,所以我们为它创建了一个类。非常感谢道格拉斯。我会看看我能用这个做什么
【解决方案4】:

像这样?

            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)))

随便玩玩

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 2018-10-16
    相关资源
    最近更新 更多