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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulHewsCommented:
Dim Seconds As String = GetSeconds(CurrentQualifierTotalTime).ToString("00")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.