Click here to Skip to main content
15,914,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two times stored in two strings like

string t1="3:20 PM";
string t2="10:30 PM";


I want to take difference between these two times

How can i do this?
Posted

C#
string t1 = "3:20 PM";
string t2 = "10:30 PM";
DateTime dt1;
DateTime.TryParse(t1, out dt1);
DateTime dt2;
DateTime.TryParse(t2, out dt2);
TimeSpan span = dt2 - dt1;
 
Share this answer
 
Comments
Hiren solanki 21-Jan-11 6:50am    
+5 to you too for being correct.
Sergey Alexandrovich Kryukov 21-Jan-11 13:51pm    
This answer is better - my 5.
No wonder, to get better you need to go slower.
thatraja 21-Jan-11 22:32pm    
Better one, Jens
C#
TimeSpan tp = Convert.ToDateTime(t2).Subtract(Convert.ToDateTime(t1));
        int hr = tp.Hours;
        int min = tp.Minutes;
        int sec = tp.Seconds;
 
Share this answer
 
Comments
JF2015 21-Jan-11 6:49am    
Damn, you were faster. 5+ for being so quick!
Sergey Alexandrovich Kryukov 21-Jan-11 13:52pm    
You should agree the way after JF2015 is better: clarity, supportability.
You get my 5 anyway.

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