Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have two strings as follows.

11:16:18:304
11:17:32:007

I want to consider those two in time format and find the difference in milliseconds. Any comments on how to do this?

I tired,

C#
TimeSpan ts = TimeSpan.Parse("11:16:18:304");


however the first colon returns an error and looking for a dot instead.

Thanks in advance!
Posted
Comments
Karthik_Mahalingam 7-Jan-14 3:43am    
you mean 11 is hour ??
CodingLover 7-Jan-14 3:49am    
Yes it is
Karthik_Mahalingam 7-Jan-14 3:58am    
check my solution.

C#
string format = "HH:mm:ss:fff";
       DateTime from = DateTime.ParseExact("11:16:18:304", format, System.Globalization.CultureInfo.InvariantCulture);
       DateTime to = DateTime.ParseExact("11:17:32:007", format, System.Globalization.CultureInfo.InvariantCulture);
       var totalmilliseconds = (to - from).TotalMilliseconds;
 
Share this answer
 
C#
DateTime t1 = DateTime.ParseExact("11:16:18:304", "HH:mm:ss:fff", null);
DateTime t2 = DateTime.ParseExact("11:17:32:007", "HH:mm:ss:fff", null);
TimeSpan t = t2 - t1;

Happy Coding!
:)
 
Share this answer
 
The format declaration for the string passed to TimeSpan.Phrase is this [ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws].
so your string in the wrong format it should be 11:16:18.304 and 11:17:32.007!
 
Share this answer
 
C#
string one="11:16:18:304";
string two="11:17:32:007";

System.DateTime dt1=Convert.ToDateTime(one);
System.DatTime dt2=Convert.ToDateTime(two);

TimeSpan ts=new TimeSpan();
ts=dt1.Subtract(dt2);//ts will give the answer in a numeric term


//Then convert ts into milliseconds by using fff timespan format string...

Have a look at the below URL too:
http://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx[^]

Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900