在 VB.net 中减去时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22083334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Subtract time in VB.net
提问by Nimitz E.
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"
在我的 checkouttime.tag 上有一个在这种格式下的时间值“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
提前致谢
采纳答案by OneFineDay
This will try to parse your DateTimestring. You may need to save a date with time that you save to your Tagproperty.
这将尝试解析您的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
回答by chris_techno25
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)
回答by Nimitz E.
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.
谢谢你的回答伙计们。

