how to achieve timespan to string conversion?

InfantPro'Aravind' picture InfantPro'Aravind' · Mar 5, 2010 · Viewed 10k times · Source

I tried searching here, but it couldn't help me much ..
I want to convert time_span to string, I don't want to return the timespan in days .. but only HH:mm:ss. How to achieve that?

My sample code is here:

              String time_span_par = "06:12:40";
              String time_str = "18:13:59";
              TimeSpan time_span_var = TimeSpan.Parse(time_span_par);
              TimeSpan time_span = TimeSpan.Parse(time_str);

              time_span = time_span.Add(time_span_var);
              string temp = time_span.ToString("HH:mm:ss");

Answer

Adriaan Stander picture Adriaan Stander · Mar 5, 2010

Try using

DateTime d = new DateTime(time_span.Ticks);
string time = d.ToString("HH:mm:ss");