469,610 Members | 1,826 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Convert hrs, mins, ms to seconds

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!
--
Sheldon
Jul 12 '08 #1
3 5936
String.Split and mytimestring.substring are your friends.

"Sheldon" <Sh*****@discussions.microsoft.comwrote in message
news:50**********************************@microsof t.com...
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!
--
Sheldon

Jul 12 '08 #2
Sheldon wrote:
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))

--
Göran Andersson
_____
http://www.guffa.com
Jul 13 '08 #3
Sheldon wrote:
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.
Jul 14 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Logan X via .NET 247 | last post: by
2 posts views Thread by Rudi Groenewald | last post: by
16 posts views Thread by Khuong Dinh Pham | last post: by
7 posts views Thread by tshad | last post: by
10 posts views Thread by =?Utf-8?B?Um95?= | last post: by
2 posts views Thread by maxin | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.