我想减去两次。
Sub notifier(checkouttime As Label)
Dim checktime As New DateTime(checkouttime.Tag)
Dim currenttime As New DateTime(DateAndTime.TimeOfDay.ToString("hh:mm:ss"))
Dim balance As TimeSpan = checktime - currenttime
End Sub
我的checkouttime.tag上的的时间值格式为“hh:mm:ss”
我必须以相同的格式获取今天的当前时间并实现它,但当我需要减去它们时,我收到错误。
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "08:00:58" to type 'Long' is not valid.
提前致谢
答案 0 :(得分:0)
这将尝试解析您的DateTime
字符串。您可能需要将日期与保存到Tag
属性的日期保存在一起。
Private Function Notifier(checkouttime As Label) As String
Dim checktime As DateTime
If DateTime.TryParse(checkouttime.Tag.ToString, checktime) Then
Dim currenttime As Date = Date.Now
Return (currenttime - checktime).ToString()
End If
Return "Bad Time String"
End Sub
答案 1 :(得分:0)
尝试这种格式......
Dim date1 As Date = #2/14/2014 9:35:04 AM#
Dim date2 As Date = #2/28/2014 12:30:54 PM#
Dim duration As TimeSpan = date1 - date2
MsgBox(duration.ToString)
答案 2 :(得分:0)
没关系,我已经为我的问题找到了另一个类似的解决方案,这就是我如何解决它。
Sub notifier(checkouttime As Label)
Dim checktime As String = checkouttime.Tag
Dim splitcheck = Split(checktime, ":")
Dim currenttime As String = CStr(DateAndTime.TimeOfDay.ToString("hh:mm:ss"))
Dim splitcurrent = Split(currenttime, ":")
Dim checkMinutes, currentMinutes As Integer
checkMinutes = CDbl(splitcheck(0)) * 60 + CDbl(splitcheck(1)) + CDbl(splitcheck(2)) / 60
currentMinutes = CDbl(splitcurrent(0)) * 60 + CDbl(splitcurrent(1)) + CDbl(splitcurrent(2)) / 60
'Dim balance As String = checkkresult - currentresult
MsgBox(checkMinutes & " " & currentMinutes & ". While current time is: " & currenttime)
End Sub
我将时间转换为分钟以实现我的目标。
感谢您的回答。