在VB中将日期转换为小时

时间:2014-07-25 02:36:51

标签: vb.net datetime computation

如何计算日期到小时?例如,我想计算July 23 to the current date(July 25 in our country).

期间过去的小时数

像这样输出:

Total Hours past: 48 Hours

2 个答案:

答案 0 :(得分:0)

我怀疑表面下方还有更多潜伏在你的代码中的日期。如果一个日期来自另一个国家,您必须考虑到这一点。没有代码就很难清楚。

Dim thisDate As DateTime = #2/10/2010#
Dim thatDate As DateTime = #10/21/2013#

Dim ts1 As TimeSpan = thatDate.Subtract(thisDate)
' or:
' Dim ts1 As TimeSpan = thatDate - thisDate
Console.WriteLine("{0} Days or {1} hours have passed", 
        ts1.TotalDays, ts1.TotalHours)

输出:

1349 Days or 32376 hours have passed

答案 1 :(得分:0)

Private Sub days_calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim d1 As Date = "24/07/2014" ' From date
    Dim d2 As Date = "24/08/2014" ' To date
    MsgBox("Date difference in days : " & (d2 - d1).TotalDays & "Days")
    MsgBox("Date difference in hours : " & (d2 - d1).TotalHours & "Hours")
    MsgBox("Date difference in minutes : " & (d2 - d1).TotalMinutes & "Minutes")
End Sub