我正在运行一个应用程序,我可以在其中获得所需的时间,但是当我尝试从另一个值中扣除一个值时,会出现错误消息,指出从日期类型转换不会发生在时间跨度上。我使用的变量如下所示:
Dim downtime As TimeSpan
Dim uptime As TimeSpan
Dim errortime As String() = lb_critical.SelectedItem.Split({" | "}, StringSplitOptions.RemoveEmptyEntries)
Dim errortimefound As DateTime = Convert.ToDateTime(errortime(0).ToString())
downtime = DateTime.Now - errortimefound
uptime = DateTime.Now - Convert.ToDateTime(downtime.ToString())
当我运行这个时,我得到了停机变量的错误。
为了尝试解决,我尝试将变量转换为所有DateTime,然后扣除它们,看看互联网但是没有成功。
任何人都可以确定我在这里做错了什么。
感谢Andrew提供改进的代码,但不幸的是我仍然收到错误。请参阅下图:
答案 0 :(得分:0)
以下是工作方法的示例:
Private Sub Button54_Click(sender As System.Object, e As System.EventArgs) Handles Button54.Click
getTimes("wibblewoffle")
getTimes("2016-06-10 08:55:12")
End Sub
Private Sub getTimes(foundTime As String)
Dim errortimefound As DateTime
If DateTime.TryParse(foundTime, errortimefound) Then
Dim downtime As TimeSpan = DateTime.Now.Subtract(errortimefound)
Dim uptime As TimeSpan = DateTime.Now.Subtract(Convert.ToDateTime(downtime.ToString()))
'do your magic here
Else
MessageBox.Show(String.Format("This is not a valid date time: =>[{0}]", foundTime))
End If
End Sub