這種功能用不到TIMER,TIMER控件用在這種地方也不適合。(假如你所統(tǒng)計(jì)的時(shí)間很短,在幾分中內(nèi)話,可以使用,假如你統(tǒng)計(jì)的時(shí)間很長(zhǎng):幾小時(shí)、幾天幾夜,建議改用以下方式):
專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)沈丘免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
在你需要開(kāi)始計(jì)時(shí)的地方加入一個(gè)記錄當(dāng)前時(shí)間,在你想結(jié)束的地方也得到一個(gè)當(dāng)前時(shí)間。然后將兩個(gè)時(shí)間相減。
希望以上思路可以幫到你。
Public Class Form1
Private Past As Date
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Past = Now
IO.File.AppendAllText("test.txt", Past.ToString, System.Text.Encoding.Default)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Now1 As Date = CDate(IO.File.ReadAllText("test.txt", System.Text.Encoding.Default))
Dim Now2 As Date = Now
MsgBox("從" Now1.ToString "到" Now2.ToString "經(jīng)過(guò)了" CStr(CDate((Now2 - Now1).ToString)))
End Sub
實(shí)際情況修改下。
End Class
一樓風(fēng)中的奶牛的做法很棒,建議樓主采納他的回答。
只是其中有一個(gè)Bug,就是“倒數(shù)第二次累計(jì)分鐘數(shù)為59,累計(jì)秒數(shù)達(dá)到60的時(shí)候,會(huì)出現(xiàn)**:60:**”。例如{"10:57:35", "2:02:51"},我這里做了修改,供參考。
另外,AddByInts中不需要進(jìn)行循環(huán)和Select判斷,可以簡(jiǎn)化。
同時(shí),為了說(shuō)明類和結(jié)構(gòu)的關(guān)系,這里改用類。
也請(qǐng)一樓風(fēng)中的奶牛勿怪罪在下??!
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Dim str As String() = {"10:30:35", "2:02:51", "17:00:35", "2:02:51"}
Dim str As String() = {"10:57:35", "2:02:51"}
Dim time As TimeSerial = New TimeSerial
For Each dt As String In str
time.Add(dt)
Next
MsgBox(time.ToString)
End Sub
End Class
Public Class TimeSerial
Public Hour As Integer ' 時(shí)間的小時(shí)部分
Public Minute As Integer ' 時(shí)間的分鐘部分
Public Second As Integer ' 時(shí)間的秒鐘部分
' 合并添加新的時(shí)間值
' param name="value"文本時(shí)間格式(00:00:00)/param
Public Sub Add(ByVal value As String)
Dim arr As String() = Split(value, ":")
Dim dts(arr.Length - 1) As Integer
For i As Integer = 0 To arr.Length - 1
dts(i) = CInt(Val(arr(i)))
Next
AddByInts(dts)
End Sub
' 合并添加新的時(shí)間值
' param name="value"時(shí)間值(僅計(jì)算:時(shí)、分、秒)/param
Public Sub Add(ByVal value As Date)
Dim dts(2) As Integer
dts(0) = value.Hour
dts(1) = value.Minute
dts(2) = value.Second
AddByInts(dts)
End Sub
Private Sub AddByInts(ByVal value As Integer())
Second += value(2)
If Second = 60 Then
Minute += Int(Second / 60)
Second = Second Mod 60
End If
Minute += value(1)
If Minute = 60 Then
Hour += Int(Minute / 60)
Minute = Minute Mod 60
End If
Hour += value(0)
End Sub
' 獲取時(shí)間的文本格式(00:00:00)
Public Overrides Function ToString() As String
Return Hour ":" Minute ":" Second
End Function
End Class