Leo Elvin Lee
Leo Elvin Lee

Reputation: 189

Conversion of time to seconds

I have this conversion issue.

I have a stopwatch which counts down. Then displays it on a label so it shows like this "15:00:00" on a label(label1) counting down.

I have another time which loop every 10 seconds to lap the stopwatch and save the lapped time to another label(label2)

So how did I get 15:00:00?

I have 3 textboxes in my form, 1st is for hours, 2nd for minutes and third for sec. If I input 15 on hours, and 00 on minutes and seconds and hit button1, it automatically converts 15hours(15:00:00) to seconds which is saved in my database so instead of saving 15:00:00 it saves the TotalSeconds which is 54000 Seconds.

When my stopwatch starts, it gets the 54000 from database and again converts it to 15:00:00 that is displayed in label1.

Can I convert lapped time on label2("13:00:00") to seconds which will display the converted value yet to another label(which is now label3).?

Imports System
Imports System.Timers

Public Class ClientDashboard

Dim StopWatch As New Stopwatch
Dim CountDown As TimeSpan
Dim IsRunning As Boolean = False
Private Shared timer As System.Timers.Timer

Private Sub ClientDashboard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Sets dashboard to right
    Me.Size = New System.Drawing.Size(317, 900)
    Dim x As Integer
    Dim y As Integer
    x = Screen.PrimaryScreen.WorkingArea.Width - 317
    y = Screen.PrimaryScreen.WorkingArea.Height - Screen.PrimaryScreen.WorkingArea.Height
    Me.Location = New Point(x, y)

    'get time of user
    cn = New ADODB.Connection
    Call conDB()
    cn.Open()
    Dim rs As New ADODB.Recordset
    rs.Open("select * from tb_registration where=st_acc_number= '" & id_lbl.Text & "'", cn, 0, 3)
    iduser_lbl.Text = "'" & rs("st_name").Value & "'""'" & rs("st_lname").Value & "'"
    UserTotal.Text = rs("st_totaltimeleft").Value

    'Start stopwatch
    StopWatch.Start()
    synchroUpdate.Enabled = True
    synchroUpdate.Start()
    Dim numSecs As Integer
    Integer.TryParse(UserTotal.Text, numSecs)
    CountDown = TimeSpan.FromSeconds(numSecs)

End Sub

Private Sub synchro_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles synchro.Tick


End Sub

'--------------------->>>> Methord 2 <<<<----------------------
Private Sub DispElaps(ByVal ts As TimeSpan, ByVal lbl As Label)
    lbl.Text = String.Format("{0:00}    :    {1:00}    :    {2:00}", _
                             Math.Floor(ts.TotalHours), _
                             ts.Minutes, _
                             ts.Seconds, _
                             ts.Milliseconds)
End Sub

Private Sub synchroUpdate_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles synchroUpdate.Tick
    synchroUpdate.Interval = 100
    'If countDown > stpw.Elapsed Then
    Dim elaps As TimeSpan = CountDown - StopWatch.Elapsed
    DispElaps(elaps, TimerOutput)
    'Else
    'End If
End Sub

Private Sub DoEvent()
    timer = New System.Timers.Timer(5000)
    AddHandler timer.Elapsed, AddressOf AC
    timer.AutoReset = True
    timer.Enabled = True
End Sub
'Address of event
Private Sub SaveEvent2(ByVal sender As System.Object, ByVal e As EventArgs)
    AutoUpdate_Button.PerformClick()
End Sub
'Event Handler
Private Sub AC()
    If Me.InvokeRequired Then
        Me.Invoke(New MethodInvoker(AddressOf AC))
    Else
        Me.AutoUpdate_Button.PerformClick()
    End If
End Sub

Private Sub logoutBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles logoutBTN.Click

End Sub

End Sub

End Class

Upvotes: 0

Views: 254

Answers (1)

Mark Hurd
Mark Hurd

Reputation: 10931

Your code seems to use good names for labels, but your question has not been updated to correspond.

Nevertheless, can you just use TimeSpan.TotalSeconds similar to where where you're already using TimeSpan.TotalHours?

Upvotes: 1

Related Questions