VB .NET - Add leading zero to Hours, Minutes, Seconds

Posted on 2007-10-21
Last Modified: 2008-01-09

I have a counter that starts from 00 hours, (as one label),  00 minutes (as one label), and 00 seconds (as one label) except that when the counter starts It takes the 00 and makes them 0 (for numbers 0 - 9).  I need to be able to make the zero stay (while in that 0 - 9 range).  I think I need to pass the integer to a string and pad it with an extra 0 but for the life of me can't remember how to do this.

Any help would be greatly appreciated.
Example Snippet:

Dim Hours, Minutes, Seconds As Integer

Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick

        Seconds = Seconds + 1
        SecondsLabel.Text = Seconds
        MinutesLabel.Text = Minutes
        HoursLabel.Text = Hours
        If SecondsLabel.Text = 60 Then Minutes = Minutes + 1
        If SecondsLabel.Text = 60 Then Seconds = 0
        If MinutesLabel.Text = 60 Then Hours = Hours + 1
        If MinutesLabel.Text = 60 Then Minutes = 0

 End Sub
Question by:Peter Frost
    LVL 85

    Accepted Solution

    Use Integer.ToString():

            SecondsLabel.Text = Seconds.ToString("00")
            MinutesLabel.Text = Minutes.ToString("00")
            HoursLabel.Text = Hours.ToString("00")
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Though this kind of "timer" is going to get inaccurate over time as the Tick() event does not fire at exactly the Interval you specify...

    Instead, consider using the DateTime and TimeSpan Classes like this:

    Public Class Form1

        Private startDT As DateTime

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            startDT = DateTime.Now
            Timer1.Interval = 500 ' how often we update the clock...not used to calculate the time!
        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim ts As TimeSpan = DateTime.Now.Subtract(startDT)
            Label1.Text = ts.Hours.ToString("00") & ":" & ts.Minutes.ToString("00") & ":" & ts.Seconds.ToString("00")
        End Sub

    End Class

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now