How do I format a string, from a timespan, to have 2 digits (i.e. 2 should be 02)

Friends,

I need help with "padding a string", with a leading 0 for seconds < 10.

I get my seconds String from this:
Dim Seconds As String = GetSeconds(CurrentQualifierTotalTime)

  Public Function GetSeconds(ByVal ElapsedSessionTime As Decimal)
        Dim ts As TimeSpan
        ts = TimeSpan.FromSeconds(ElapsedSessionTime)
        Return ts.Seconds
    End Function
Then I tried without success the following:

Seconds = String.Format(Seconds, "#0")

Thanks in advance!
indy500fanAsked:
Who is Participating?
 
PaulHewsConnect With a Mentor Commented:
Dim Seconds As String = GetSeconds(CurrentQualifierTotalTime).ToString("00")
0
 
indy500fanAuthor Commented:
Paul,

Nope same result.
0
 
PaulHewsCommented:
Make sure you return the right type...  This shows a message box with 03, so it works.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox(GetSeconds(3).ToString("00"))
    End Sub
 
 
    Public Function GetSeconds(ByVal ElapsedSessionTime As Decimal) As Integer
        Dim ts As TimeSpan
        ts = TimeSpan.FromSeconds(ElapsedSessionTime)
        Return ts.Seconds
    End Function

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
indy500fanAuthor Commented:
Well Sure enough, when I do it in another NEW project, your method works; however, it won't work in my current solution.  Any suggestions?
0
 
indy500fanAuthor Commented:
Look here, watching the variable, at a breakpoint, it shows 2, not 02.
Seconds.JPG
0
 
PaulHewsCommented:
Did you fix the GetSeconds function to return an integer?  In your original code, you did not specify a return type.... (One reason I always use Option Strict On...)
0
 
PaulHewsCommented:
Yep, this makes the difference...

    Public Function GetSeconds(ByVal ElapsedSessionTime As Decimal) As Integer
        Dim ts As TimeSpan
        ts = TimeSpan.FromSeconds(ElapsedSessionTime)
        Return ts.Seconds
    End Function
0
 
indy500fanAuthor Commented:
And it does!  Thank you!
0
All Courses

From novice to tech pro — start learning today.