Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Need help converting seconds into HH:MM:SS


Reply
 
Thread Tools Display Modes
  #1  
Old 04-03-2010, 08:46 AM
silens silens is offline
Newcomer
 
Join Date: Apr 2010
Location: Israel
Posts: 3
Unhappy 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.
Code:
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
    End Sub

    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
            MsgBox("Done!")
        Else
            '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)
            End If
            If m < 10 Then
                mm = "0" & Int(m)
            Else : mm = Int(m)
            End If
            If s < 10 Then
                ss = "0" & Int(s)
            Else : ss = Int(s)
            End If
            Label6.Text = hh & ":" & mm & ":" & ss
        End If
    End Sub
End Class
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!
Paul
Reply With Quote
  #2  
Old 04-03-2010, 09:13 AM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 1,880
Default

Have you considered using the .Net functionality rather than the legacy VB methods?

Typing this without access to VB so it might be off a tad but the first bit could be done as
Code:
 Label1.Text = "Time now:" & Now()
        seconds = Integer.Parse(TextBox1.Text)  'Should be using Option Strict On
        timeNext = Date.Now.AddSeconds(seconds)
  Label2.Text = "The time in " & seconds & " seconds:" & timenext
        Timer1.Enabled = True
        Timer1.Interval = 1000
While the difference could be done like
Code:
Dim diff as TimeSpan = TimeNext.Subtract(Date.Now)
Label6.Text = diff.ToString("hh:mm:ss")
Hope that help, just excuse an slight typos that might exist.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 04-03-2010, 09:35 AM
silens silens is offline
Newcomer
 
Join Date: Apr 2010
Location: Israel
Posts: 3
Default

Thank you for the quick reply PlausiblyDamp!
However, when using the difference line you've suggested, I've received this error:
Code:
Overload resolution failed because no accessible 'ToString' accepts this number of arguments.
Reply With Quote
  #4  
Old 04-03-2010, 10:37 AM
silens silens is offline
Newcomer
 
Join Date: Apr 2010
Location: Israel
Posts: 3
Default

I believe I've found the solution with some searching using the above material
Code:
Option Strict On
Public Class Form1
    Public seconds As Integer
    Public timenext As Date
    Public str 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 = Integer.Parse(TextBox1.Text)  'Should be using Option Strict On
        timenext = Date.Now.AddSeconds(seconds)
        Label2.Text = "The time in " & seconds & " seconds:" & timenext
        Timer1.Enabled = True
        Timer1.Interval = 1000
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim diff As TimeSpan = timenext.Subtract(Date.Now)
        Label3.Text = New DateTime(diff.Ticks).ToString("HH:mm:ss")
    End Sub
End Class
Thanks again!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->