Sottrae il tempo in vb.net
-
24-12-2019 - |
Domanda
Sto cercando di sottrarre due volte.
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
.
sul mio checkouttime.tag ha un valore di tempo di sotto questo formato "hh: mm: ss"
E devo ottenere l'ora corrente per oggi con lo stesso formato e lo ottengo ma quando ho bisogno di sottrarli, sto ricevendo un errore.
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.
.
Grazie in anticipo
Soluzione
Proverà ad analizzare la tua stringa DateTime
.Potrebbe essere necessario salvare una data con il tempo che si salva nella tua proprietà 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
. Altri suggerimenti
Prova questo tipo di formato ...
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, ho trovato un'altra soluzione simile per il mio problema ed ecco come lo risolvo.
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
.
Ho convertito il tempo in minuti per raggiungere i miei obiettivi.
Grazie per la tua risposta ragazzi.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow