Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何将DateTime转换为整数并检查差异?_Vb.net - Fatal编程技术网

Vb.net 如何将DateTime转换为整数并检查差异?

Vb.net 如何将DateTime转换为整数并检查差异?,vb.net,Vb.net,我试着比较一下一天中两次的差异。我得到UTC格式的时间。我是否必须将DateTime转换为整数才能获得差异?如果是,如何转换 这是我的密码 Dim ct As DateTime = DateTime.UtcNow Dim int_ct As Integer = Convert.ToInt32(ct) MsgBox(ct + " | " + int_ct) If (System.IO.File.Exists(System.IO.Path.GetTem

我试着比较一下一天中两次的差异。我得到UTC格式的时间。我是否必须将DateTime转换为整数才能获得差异?如果是,如何转换

这是我的密码

Dim ct As DateTime = DateTime.UtcNow
        Dim int_ct As Integer = Convert.ToInt32(ct)
        MsgBox(ct + " | " + int_ct)
        If (System.IO.File.Exists(System.IO.Path.GetTempPath.ToString + "\tempfile.data")) Then
            Dim time As DateTime = System.IO.File.ReadAllText(System.IO.Path.GetTempPath.ToString + "\tempfile.data")
            Dim int_time As Integer = Convert.ToInt32(time)
            If ((int_ct - unlockedtime) < int_time) Then 'unlockedtime is Int variable
                Return False
            Else
                Return True
            End If
        End If
Dim ct As DateTime=DateTime.UtcNow
Dim int\u ct为整数=转换为32(ct)
MsgBox(ct+“|”+int\U ct)
如果(System.IO.File.Exists(System.IO.Path.GetTempPath.ToString+“\tempfile.data”)),则
Dim time As DateTime=System.IO.File.ReadAllText(System.IO.Path.GetTempPath.ToString+“\tempfile.data”)
Dim int_time As Integer=转换为.ToInt32(时间)
如果((int_ct-unlockdtime)
您不需要进行任何时区转换,只要在捕获日期/时间值时确保这两个日期/时间值位于同一时区即可(尽管我认为在两次测量之间系统时区发生变化的情况下,使用
UtcNow
可能是明智的

无需将值转换为
Int32
即可获得差值,只需使用
DateTime.Subtract
方法:

Dim old As DateTime = GetOldDateTime()
Dim now As DateTime = DateTime.UtcNow
Dim diff As TimeSpan = now.Subtract( old )

If diff.TotalSeconds > someSecondsValue Then
    DoSomething()
End If

您不需要进行任何时区转换,只需确保在捕获日期/时间值时,这两个日期/时间值位于同一时区(尽管我认为使用
UtcNow
可能是明智的,以防系统时区在两次测量之间发生变化

无需将值转换为
Int32
即可获得差值,只需使用
DateTime.Subtract
方法:

Dim old As DateTime = GetOldDateTime()
Dim now As DateTime = DateTime.UtcNow
Dim diff As TimeSpan = now.Subtract( old )

If diff.TotalSeconds > someSecondsValue Then
    DoSomething()
End If

你试过Datetime.Compare(dt1,dt2)吗?这取决于你需要多少细节,它可能适合你

以下是有关该功能的更多信息:

您是否尝试过Datetime.Compare(dt1,dt2)?这取决于您需要多少详细信息,这可能适合您

以下是有关该功能的更多信息:

正如Dai所说,您可能想要比较差异,但如果您只想要日期差异,您可以使用以下方法:

DateDiff(DateInterval.Minute, Date1, date2)

这将差异作为整数返回,您可以将所需的任何日期间隔设置为第一个参数。

正如Dai所说,您可能需要比较差异,但如果您只需要日期差异,则可以使用以下方法:

DateDiff(DateInterval.Minute, Date1, date2)
这将以整数形式返回差值,您可以将所需的任何日期间隔设置为第一个参数。

我将“someSecondsValue”作为一个整数变量。是否有方法将该整数值转换为TimeSpan以进行比较,因为我遇到一个错误,无法比较这两个值。我有“someSecondsValue”作为一个整数变量。有没有办法将整数值转换为TimeSpan来比较它,因为我得到一个错误,我无法比较这两个值。