【发布时间】:2016-01-18 11:07:59
【问题描述】:
我有两个从 mysql 检索到的日期时间变量。 一个等于'now()',另一个代表最后一次更改行。
我需要比较这两个日期时间值来获得两者之间的差异。
这就是我正在尝试的(我从 Mysql 中读取的内容是由定制的 Web api 处理的)...
'get the current time
Dim nowtime As DateTime = readsql("now()", "hosts", "")
Console.WriteLine("NOW IS : " & nowtime)
'read in all the rows
Dim hosts() As String = readsql("hostname", "hosts", "chk_ping=1").ToString.Split("|")
For Each host As String In hosts
If Not host.Contains("No results found") Then
Dim thishostip As String = readsql("IP", "hosts", "hostname='" & host & "'")
Dim thishostlastpoll As DateTime = readsql("last_poll", "hosts", "hostname='" & host & "'")
If thishostip.Contains("No results found") Then Exit For
Console.WriteLine("HOST TIME IS:" & thishostlastpoll.ToString)
Dim timedifference As Integer = DateTime.Compare(nowtime, thishostlastpoll)
Console.WriteLine("Time diff for " & host & " is : " & timedifference)
End If
Next
我的日期时间值是这样进来的......
18/01/2016 10:53:00
18/01/2016 10:52:52
并且不会抛出任何错误/异常,但无论差异如何,'timedifference' 永远只有 1。
我怀疑它与日期时间的格式有关,这不是 VB 想要的,但我找不到任何可以将 mysql 转换为 vb 日期时间的东西,反之亦然。
任何方向都会被应用!提前致谢!
【问题讨论】:
-
不是“格式”,日期没有格式。一旦你将这 2 个值放入 DateTime 变量中,它们就是 Net DateTime 变量,如果 Net 不喜欢它们的某些东西,你会得到一个错误。如果我比较这两个值,它工作正常。您能否显示
readsql并告诉我们数据库中使用的数据类型 -
readsql 是对处理所有数据库交互的 php api 的 http 调用。我想我会使用计时器而不是比较实际时间,然后一起否定这个问题:)