views:

493

answers:

6

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");
+8  A: 

Try using

DateTime d = new DateTime(time_span.Ticks);
string time = d.ToString("HH:mm:ss");
astander
hello @astander, Your answer is acceptable too .. But I personally liked one-liner code ..
infant programmer
@infant programmer, this one can be one-liner too, you can combine the two expressions in one line: `string time = new DateTime(time_span.Ticks).ToString("HH:mm:ss");`
CMS
@cms, ok .. then accepted. I was just avoiding declaration of another variable.
infant programmer
+4  A: 

This should work:

string temp = string.Format("{0}:{1}:{2}",
    time_span.Hours.ToString(), time_span.Minutes.ToString(),
    time_span.Seconds.ToString());

As per comment if you want the double digits you could do:

string temp = string.Format("{0}:{1}:{2}",
    time_span.Hours.ToString("00"), time_span.Minutes.ToString("00"),
    time_span.Seconds.ToString("00"));

Edited:as per jimmy's comment,

string temp = string.Format("{0:00}:{1:00}:{2:00}",time_span.Hours, time_span.Minutes, time_span.Seconds);
Kelsey
Won't that give you things like "1:2:6" for <10 portions?
Marc Gravell
Yes it will :) Added the formatting for double digits.
Kelsey
string.Format("{0:00}:{1:00}:{2:00}",time_span.Hours, time_span.Minutes, time_span.Seconds);
Jimmy
+1  A: 

Simply convert the value of ticks into a DateTime and then use its ToString()

var date1 = DateTime.Now;
var date2 = DateTime.Now.AddSeconds( -1000 );
var diff = date1 - date2;
var temp = new DateTime( diff.Ticks ).ToString( "HH:mm:ss" )
Thomas
+1, thanks for the response,
infant programmer
+2  A: 

Try this:

    time_span = time_span.Add(time_span_var);
    string temp = time_span.ToString();
    temp = string.Format("{0}:{1}:{2}", time_span.TotalHours, time_span.TotalMinutes, time_span.TotalSeconds);

Edit After I read your comment on your question, that is you need to display zero hours for new days, my answer will give you total hours, minutes and seconds, not what you want.

(+1) Kelseys ;)

Sameh Serag
You can't use `TotalHours`, `TotalMinutes`, etc as they are independently calculated all encompassing of the value. Also just doing a ToString() on the timespan outputs: `1.00:26:3` where my version outputs: `00:26:39`.
Kelsey
Thanks Kelsey ;) Your answer really is the best for her question. I edited my answer above before my page updated with your comment. Anyway, thank you.
Sameh Serag
+1  A: 
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);

TimeSpan finalTime =  (time_span_var + time_span);
Console.WriteLine(finalTime);
Console.WriteLine(finalTime - TimeSpan.FromHours(finalTime.Days * 24));
shahkalpesh
Impressive .. :-) +1
infant programmer
+2  A: 

The code I have implemented is:

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

Originally posted by Marc Gravell,

infant programmer
why did you implement one answer and accept a different one?
Jimmy
This is not my own answer .. so I don't want to take the credit, And also I would like to value others helping attitude and effort made .. :-)
infant programmer
And also the accepted solution is worth of implementation ..
infant programmer