junkone
junkone

Reputation: 1465

How can I convert string to time object for calculation

how do I format output in seconds:milliseconds format?

TimeSpan start = TimeSpan.Parse(pair.Value[3]);
TimeSpan end = TimeSpan.Parse(pair.Value[4]);
Console.WriteLine(TimeSpan.Compare( start,end));

The code here is printing the difference in seconds. how can i fix it using string.format as i dont know the convention for milliseconds?

Upvotes: 0

Views: 74

Answers (3)

Eric J.
Eric J.

Reputation: 150228

You need to look at the difference between the two timespans.

TimeSpan start = new TimeSpan(42); // 42 ticks
TimeSpan end = new TimeSpan(420000000);
TimeSpan diff = end.Subtract(start);
string ms = diff.Milliseconds.ToString();
string sec = ((int)diff.TotalSeconds).ToString();
Console.WriteLine(sec + ":" + ms);

Upvotes: 2

competent_tech
competent_tech

Reputation: 44971

How about

Console.WriteLine(end.Subtract(start).TotalMilliseconds)

Upvotes: 2

dash
dash

Reputation: 91550

If it's in seconds, you could just multiply the answer by 1000 and write that out as a string?

Otherwise, take a look at this MSDN article http://msdn.microsoft.com/en-us/library/bb882581.aspx

Upvotes: 1

Related Questions