我想比较日期和时间。 我试过date1.Date.CompareTo(date2.Date),但我不知道如何为它添加时间。 谢谢 Furqan
答案 0 :(得分:1)
如果您需要比较时间,则需要将其声明为DateTime
而不是Date
Dim dt1 As DateTime
Dim dt2 As DateTime
dt1 = Now
dt2 = Now.AddMilliseconds(5)
Dim dt3 As DateTime
MessageBox.Show(dt2.Compare(dt2, dt1).ToString())
答案 1 :(得分:0)
如果您希望这两个日期之间的差异在几天,几小时,几分钟等内,请检查DateDiff。
或使用d.AddDays,d.AddHours等,其中d是日期类型的变量
答案 2 :(得分:-1)
Dim dt1 As Date = Now
'Dim dt2 As Date = DateAdd(DateInterval.Year, 1, dt1) 'Add 1 year
'Dim dt2 As Date = DateAdd(DateInterval.Month, 1, dt1) 'Add 1 month
Dim dt2 As Date = DateAdd(DateInterval.Day, 1, dt1) 'Add 1 day
'Dim dt2 As Date = DateAdd(DateInterval.Hour, 1, dt1) 'Add 1 hour
'Dim dt2 As Date = DateAdd(DateInterval.Minute, 1, dt1) 'Add 1 minute
'Dim dt2 As Date = DateAdd(DateInterval.Second, 1, dt1) 'Add 1 second
If Format(dt1, "yyyyMMdd") < Format(dt2, "yyyyMMdd") Then
MsgBox("dt1 is lower then dt2")
ElseIf Format(dt1, "yyyyMMdd") > Format(dt2, "yyyyMMdd") Then
MsgBox("dt1 is greater then dt2")
ElseIf Format(dt1, "yyyyMMdd") = Format(dt2, "yyyyMMdd") Then
MsgBox("dt1 is equal dt2")
End If