how to achieve timespan to string conversion?
Asked Answered
G

8

9

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");
Geraldina answered 5/3, 2010 at 6:12 Comment(2)
Does that mean you want to add 18 hrs to 6 hrs? Wouldn't it be another day then?Baldhead
Ya but I don't want the day .. 0 hours is enough to get displayed ..Geraldina
E
17

Try using

DateTime d = new DateTime(time_span.Ticks);
string time = d.ToString("HH:mm:ss");
Elizabethelizabethan answered 5/3, 2010 at 6:18 Comment(4)
hello @astander, Your answer is acceptable too .. But I personally liked one-liner code ..Geraldina
@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");Pythagoras
@cms, ok .. then accepted. I was just avoiding declaration of another variable.Geraldina
if using framework 4, you can now use a literal string directly on the TimeSpan instance making for much easier to read code: time_span.ToString(@"hh\:mm\:ss")Bottali
M
6

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);
Mango answered 5/3, 2010 at 6:18 Comment(2)
Won't that give you things like "1:2:6" for <10 portions?Akkerman
string.Format("{0:00}:{1:00}:{2:00}",time_span.Hours, time_span.Minutes, time_span.Seconds);Implicit
W
3

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 ;)

Wiretap answered 5/3, 2010 at 6:26 Comment(2)
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.Mango
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.Wiretap
G
3

The code I have implemented is:

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

Originally posted by Marc Gravell,

Geraldina answered 5/3, 2010 at 6:42 Comment(2)
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 .. :-)Geraldina
While i hate the cost of casting (to get my timespan i actually have to cast a double already) this is a far more elegant piece of codeStrode
B
3

There is a much simpler way of doing this now (albeit only using framework 4), you just need to use the string literally, and you can do it directly on the TimeSpan instance.

time_span.ToString(@"hh\:mm\:ss")

That will output

00:26:39

Helpful now for people stumbling across this (like myself).

Cheers :)

Bottali answered 6/6, 2012 at 9:13 Comment(0)
S
1

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" )
Simonetta answered 5/3, 2010 at 6:21 Comment(0)
B
1
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));
Baldhead answered 5/3, 2010 at 6:34 Comment(0)
K
0

If the number of days is irrelevant then you have the solution, however I came across this answer searching for a conversion that gave hours in total, so 36 hours would need to be displayed as 36:00:00. Using some of the hints above this is what I came up with:

SomeLabel.Text = Math.Floor(ts.TotalHours).ToString() + ":" + ts.Minutes.ToString("D2") + ":" + ts.Seconds.ToString("D2");

Total Hours is always rounded down, minutes and seconds are padded to be 2 digits (00 - 09)

Kathlyn answered 31/8, 2011 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.