convert time string to number of seconds

T

tony

Hello!

I have this method ConvStdTimeToSec here that convert format hh:mm:ss into
total number of seconds and return it.
I just wonder if there exist a metod in the .NET framework that can be used
instead.

private int ConvStdTimeToSec(string time)
{
return int.Parse(time.Substring(0,2)) * 3600 +
int.Parse(time.Substring(3,2)) * 60 + int.Parse(time.Substring(6,2));
}

//Tony
 
T

Tom Porterfield

tony said:
Hello!

I have this method ConvStdTimeToSec here that convert format hh:mm:ss into
total number of seconds and return it.
I just wonder if there exist a metod in the .NET framework that can be
used instead.

private int ConvStdTimeToSec(string time)
{
return int.Parse(time.Substring(0,2)) * 3600 +
int.Parse(time.Substring(3,2)) * 60 + int.Parse(time.Substring(6,2));
}

DateTime dt = DateTime.ParseExact(time, "HH:mm:ss",
System.Globalization.CultureInfo.CurrentCulture);
return dt.TimeOfDay.TotalSeconds;
 
B

Barry Kelly

tony said:
Hello!

I have this method ConvStdTimeToSec here that convert format hh:mm:ss into
total number of seconds and return it.
I just wonder if there exist a metod in the .NET framework that can be used
instead.

private int ConvStdTimeToSec(string time)
{
return int.Parse(time.Substring(0,2)) * 3600 +
int.Parse(time.Substring(3,2)) * 60 + int.Parse(time.Substring(6,2));
}

TimeSpan.Parse(time).TotalSeconds

-- Barry
 
M

Mark Rae

I have this method ConvStdTimeToSec here that convert format hh:mm:ss into
total number of seconds and return it.

You need the TimeOfDay.TotalSeconds method of the the DateTime class.
 

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