Question

I am trying to subtract two times.

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

on my checkouttime.tag has a time value of under this format "hh:mm:ss"

and I have to get the current time for today with the same format and I achieve it but when I need to subtract them I am getting an error.

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.

Thanks in advance

Was it helpful?

Solution

This will try to parse your DateTime string. You may need to save a date with time that you save to your Tag property.

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

OTHER TIPS

Try this kind of format...

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)

Nevermind, I have found another similar solution for my problem and here is how I solve it.

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

I converted the time to minutes to achieve my goals.

Thanks for your answer guys.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top