vb.net 时间相减问题VB.NET codePrivate Function GetDoTime(ByVal Time1 As DateTime, ByVal Time2 As Da
vb.net 时间相减问题
VB.NET code
Private Function GetDoTime(ByVal Time1 As DateTime, ByVal Time2 As DateTime) As String Dim stime As DateTime If Time1 > Time2 Then stime = Time1 Time1 = Time2 Time2 = stime End If Dim secondvalue As Long = DateDiff("s", Time1, Time2) Dim dayvalue As Long = DateDiff("d", Time1, Time2) Dim hourvalue As Double = Fix((secondvalue - dayvalue * 24 * 3600) / 360) If hourvalue < 0 Then dayvalue = dayvalue - 1 : hourvalue = 24 + hourvalue Dim minitevalue As Double = Fix((secondvalue - dayvalue * 24 * 3600 - hourvalue * 3600) / 60) If minitevalue < 0 Then hourvalue = hourvalue - 1 : minitevalue = 60 + minitevalue Dim secvalue As Double = secondvalue - dayvalue * 24 * 3600 - hourvalue * 3600 - minitevalue * 60 If secvalue < 0 Then minitevalue = minitevalue - 1 : secvalue = 60 + secvalue GetDoTime = dayvalue & "天" & hourvalue & "小时" & minitevalue & "分" & secvalue & "秒" End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox(GetDoTime(Now, "2011-09-13 10:52:09")) Me.Dispose() End Sub
请帮一下谢谢 qq:2319246391
[解决办法] Private Function GetDoTime1(ByVal Time1 As DateTime, ByVal Time2 As DateTime) As String Dim nTime1 As DateTime Dim nTime2 As DateTime If Time1 > Time2 Then nTime2 = Time1 nTime1 = Time2 Else nTime1 = Time1 nTime2 = Time2 End If Dim SecondValue As Integer = DateDiff(DateInterval.Second, nTime1, nTime2) Dim secvalue As Long = SecondValue Mod 60 SecondValue -= secvalue Dim minitevalue As Long = (SecondValue Mod 3600) / 60 If minitevalue = 0 Then minitevalue = 60 SecondValue -= minitevalue * 60 Dim hourvalue As Long = (SecondValue Mod 3600 * 24) / 3600 If hourvalue = 0 Then hourvalue = 24 SecondValue -= hourvalue * 3600 Dim dayvalue As Long = SecondValue / (3600 * 24) Return dayvalue & "天" & hourvalue & "小时" & minitevalue & "分" & secvalue & "秒" End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox(GetDoTime1(Now, "2011-09-13 10:52:09")) MsgBox(GetDoTime1("2011-09-13 10:52:09", Now)) Me.Close() End Sub [解决办法] 是否是需要显示两个时间的比对,再显示相差几天 如果 "1天 12小 30分钟 25秒"
如果是,现在的就能实现,有很多函数,
Private Sub dd(d1 As Date, d2 As Date)
Dim ts As TimeSpan = d2 - d1 Dim ob() As Object = New Object() {ts.TotalDays, ts.TotalHours, ts.TotalMinutes, ts.TotalSeconds} Dim s As String = String.Format("{0}天 {1}小时 {2}分钟 {3}秒", ob) MessageBox.Show(s)