0

I have a timer set as a VB object in a spread sheet. It currently displays as hh:mm:ss.00. I need it to display as seconds only (no miliseconds or minutes, IE 1:30 should show as 90).

Dim StopTimer As Boolean
Dim Etime As Single
Dim Etime0 As Single
Dim LastEtime As Single

Private Sub CommandButton1_Click()
StopTimer = False
Etime0 = Timer() - LastEtime
Do Until StopTimer
Etime = Int((Timer() - Etime0) * 100) / 100
If Etime > LastEtime Then
LastEtime = Etime
Label1.Caption = Format(Etime / 86400, "hh:mm:ss.") & Format(Etime * 100 Mod 100, "00")
DoEvents
End If
Loop
End Sub

Private Sub CommandButton2_Click()
StopTimer = True
Beep
    With New MSForms.DataObject
        .SetText Label1.Caption
        .PutInClipboard
    End With
End Sub

Private Sub CommandButton3_Click()
StopTimer = True
Etime = 0
Etime0 = 0
LastEtime = 0
Label1.Caption = "00"
End Sub

I'm sure I'm simply overlooking something obvious but I'm not overly familiar with timers and formatting.

2 Answers2

1

Please refer this link:

Convert HH:MM:SS string to seconds only in javascript

And try this:

var hms = '02:04:33';   // your input string
var a = hms.split(':'); // split it at the colons

// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 

console.log(seconds);
Pang
  • 9,564
  • 146
  • 81
  • 122
Suniti Yadav
  • 393
  • 2
  • 8
0

This is probably the easiest and the most comprehensive way, as a minute has 60 seconds and one hour has 3600 seconds.

Thus 1 minute and 30 seconds would be 0*3600 + 1*60 + 30:

Public Sub TestMe()

    Dim myTime As Date
    myTime = TimeSerial(0, 1, 30)
    Debug.Print Hour(myTime) * 3600 + Minute(myTime) * 60 + Second(myTime)

End Sub

It takes 1:30 and it returns 90. You may consider writing a separate function like this one as well:

Public Function TimeToSeconds(Optional hours As Long = 0, _
                                Optional minutes As Long = 0, _
                                Optional seconds As Long = 0) As Long

    TimeToSeconds = hours * 3600 + minutes * 60 + seconds

End Function
Vityata
  • 42,633
  • 8
  • 55
  • 100