Convert hrs, mins, ms to seconds

S

Sheldon

Hello -

I have various times formatted like 00:00:00, so for example, 01:32:05 would
be one thirty-two with five milliseconds.

I need to convert this into seconds (an integer).

First of all, I have been unable to use the mid() function like I did in vb
6.

I have searched all over the internet and it seems everyone is doing just
the opposite; converting an integer to hours, minutes and seconds!!

Any help will be greatly appreciated!
 
G

Göran Andersson

Sheldon said:
Hello -

I have various times formatted like 00:00:00, so for example, 01:32:05 would
be one thirty-two with five milliseconds.

That doesn't make sense. You should have three digits as milliseconds,
not two. With two digits, it should be hundreds of seconds.
I need to convert this into seconds (an integer).

That would discard the part that is a fraction of seconds, as an integer
doesn't have any fractions.
First of all, I have been unable to use the mid() function like I did in vb
6.

I have searched all over the internet and it seems everyone is doing just
the opposite; converting an integer to hours, minutes and seconds!!

Any help will be greatly appreciated!

Use the DateTime.ParseExact method to parse the string into a DateTime
value.

If the string contains minutes, seconds and hundreds of seconds, use a
format like "MM:ss:ff". If it contains minutes, seconds and
milliseconds, use a format like "MM:ss:fff".

Dim t As DateTime = DateTime.Parse("01:32:05", "MM:ss:ff",
CultureInfo.InvariantCulture)

To get the time component of the DateTime value, subtract it with it's
date component, which gives you a TimeSpan value:

Dim s As TimeSpan = t - t.Date

To get the value as seconds, use the TotalSeconds property:

Dim sec As Double = s.TotalSeconds

If you want the seconds as an integer, use a method like Math.Floor or
Math.Round to round it the way you want, then convert it to an integer:

Dim secInt As Integer = Convert.ToInt32(Math.Floor(sec))
 
P

Phill W.

Sheldon said:
I have various times formatted like 00:00:00, so for example, 01:32:05 would
be one thirty-two with five milliseconds.

I'm not convinced that's what you actually /mean/ (how would 220
milliseconds fit this format?) but, as given, you'd want something like:

' 1 minute, 32.005 seconds
Dim sInput as String = "01:32:05"

Dim sBits as String() _
= sInput.Split( ":"c )

Dim dblSeconds as Double _
= CInt( sBits(0) ) * 60 _
+ CInt( sBits(1) ) _
+ ( CInt( sBits(2) ) / 1000 )

? dblSeconds
92.005
I need to convert this into seconds (an integer).

You can't use an Integer if you're holding milliseconds as /fractions/
of a second.
First of all, I have been unable to use the mid() function like I did in vb
6.

"Mid" and all the rest /are/ still there.

If you try the "new" String methods, though, get ready to decrement
everything you ever knew about indexing into strings - everything in
..Net is /zero/-based and far less forgiving of "running off the end" of
the string. :-(

HTH,
Phill W.
 

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

Top