04-03-2010, 09:46 AM
Join Date: Apr 2010
Need help converting seconds into HH:MM:SS
Hey guys! I'm currently coding a small app, and I got sort of stuck when I had to convert from seconds to HH:MM:SS.
This is the "solution" I came up with so far, but there's a small problem with it.
Public Class Form1
'Accept amount of seconds
Public seconds As Integer
'Time now + seconds
Public timenext As Date
'Time differences, not needed anymore due to the new solution (EXCEPT DIFF)
Public diff As Integer
Public diffm As Integer
Public diffh As Integer
'Vars to convert time from seconds to HH:MM:SS
Public s As Double
Public m As Double
Public h As Double
Public ss As String
Public mm As String
Public hh As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Time now:" & Now()
seconds = TextBox1.Text
timenext = DateAdd(DateInterval.Second, seconds, Now())
Label2.Text = "The time in " & seconds & " seconds:" & timenext
Timer1.Enabled = True
Timer1.Interval = 1000
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
diff = DateDiff(DateInterval.Second, Now(), timenext)
diffm = DateDiff(DateInterval.Minute, Now(), timenext)
diffh = DateDiff(DateInterval.Hour, Now(), timenext)
If diff = 0 Then
Timer1.Enabled = False
'The differences. All I need is diff.
Label3.Text = "The difference:" & diff
Label4.Text = "The difference in minutes:" & diffm
Label5.Text = "The difference in minutes:" & diffh
'Converting DIFF to HH:MM:SS. Int used to truncate.
h = diff / 3600
m = diff / 60
s = diff - (Int(h) * 3600 + Int(m) * 60)
'Checking for format. Adding head '0' when necessary.
If h < 10 Then
hh = "0" & Int(h)
Else : hh = Int(h)
If m < 10 Then
mm = "0" & Int(m)
Else : mm = Int(m)
If s < 10 Then
ss = "0" & Int(s)
Else : ss = Int(s)
Label6.Text = hh & ":" & mm & ":" & ss
The code works fine if the amount of seconds is 3600 (1 hour) and under, but if its above, it doesn't really work.
What can I do in such a situation?
Thanks in advance!