Formatting Seconds for Display

  • Thread starter Thread starter Ricky W. Hunt
  • Start date Start date
R

Ricky W. Hunt

Is there a built-in function in VB.NET that will take a number of seconds
(e.g. 243) and display it as: "04:03" (four hours, three minutes)?
 
why not something like this???? (haven't tested--just coding in Outlook
Express--but something similar would do just what you want

function Whatever(secs as integer) as string
return format(secs\3600,"00") & ":" & format(secs mod 3600)\60,"00") &
":" & format(((secs mod 3600)\60) mod 60,"00")
end function

HTH,

Shane
ps. how you get 4 hours and 3 minutes out of 243 seconds....I don't know..
 
SStory said:
why not something like this???? (haven't tested--just coding in Outlook
Express--but something similar would do just what you want

function Whatever(secs as integer) as string
return format(secs\3600,"00") & ":" & format(secs mod 3600)\60,"00") &
":" & format(((secs mod 3600)\60) mod 60,"00")
end function

HTH,

I wrote a routine to do it. It just seems like VB.NET would have some
built-in function to do this.
 
Is there a built-in function in VB.NET that will take a number of seconds
(e.g. 243) and display it as: "04:03" (four hours, three minutes)?

As long as you don't go over 24 hours you can cheat and do this:

Public Function HmsFromSeconds(ByVal Seconds As Integer) As String
Return New Date(2000, 1, 1).AddSeconds(Seconds).ToString("HH:mm:ss")
End Sub
 
pretty cool.
I wasn't aware of a format method in the tostring event for dates.... cool.

Shane
 
Ricky
Is there a built-in function in VB.NET that will take a number of seconds
(e.g. 243) and display it as: "04:03" (four hours, three minutes)?

Yes the TimeSpan, it has a lot of possibilities, one sample
\\\
Dim dt As TimeSpan
dt = dt.FromSeconds(243)
MessageBox.Show(dt.Minutes & ":" & dt.Seconds)
///

I hope this helps?

Cor
 
Ricky W. Hunt said:
Is there a built-in function in VB.NET that will take a number of seconds
(e.g. 243) and display it as: "04:03" (four hours, three minutes)?

Not directly, but you can combine some of the built-ins. Representing a time
interval in .NET is best done with the TimeSpan structure. The following
example illustrates this:

Dim ts As TimeSpan
ts = TimeSpan.FromSeconds(243)
MsgBox(ts.ToString)

The only drawback is that this will display your time as 00:04:03, but you
can of course slice the string afterwards. Unfortunately, there is no
overloaded ToString method as with DateTime structures. Alternatively, you
could store the interval in a DateTime structure:

Dim dtm As DateTime
dtm = dtm.AddSeconds(243)
MsgBox(dtm.ToString("mm:ss"))

This will give you the requested result.

/Jens
 
Jens Christian Mikkelsen said:
Dim dtm As DateTime
dtm = dtm.AddSeconds(243)
MsgBox(dtm.ToString("mm:ss"))

This will give you the requested result.

/Jens

Perfect. Thanks.
 
Cor Ligthert said:
Ricky


Yes the TimeSpan, it has a lot of possibilities, one sample
\\\
Dim dt As TimeSpan
dt = dt.FromSeconds(243)
MessageBox.Show(dt.Minutes & ":" & dt.Seconds)
///

I hope this helps?

Yes. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top