You must Sign In to post a response.

How to convert a string to a timespan in C#.net?

I have a string which I want to convert toa time span object
eg.
string sDuration = "00:18:09";

Now I want to assign this value to a time span object.
eg.
TImeSpan ts = new TimeSpan();
ts = (TimeSpan)sDuration;

but this type cast is not working.


Comments

Author: satyaprakash04 Jul 2008 Member Level: Silver   Points : 4

TimeSpan timeSpan = new System.TimeSpan();
bool isTimeSpan = System.TimeSpan.TryParse(sDuration , out timeSpan); (if u r working in 2003 u wont have TryParse..u can use simply Parse method.TryParse returns false if the input string is not in correct format .)..

Try it.It works

Rohan