TimeSpan ToString format

C#.Net

C# Problem Overview


Just curious, is there a format string I can use to output something like "5h 3m 30s"?

eg. (obviously wrong)

myTimeSpan.ToString("hh mm ss")

C# Solutions


Solution 1 - C#

Try:

myTimeSpan.ToString("h'h 'm'm 's's'")

(Note that even spaces need to be quoted - that's what was wrong with my first attempt.)

I'm assuming you're using .NET 4, of course - before that, TimeSpan didn't support custom format strings.

EDIT: As noted, this won't work beyond 24 hours. Also note that alternatives are available via Noda Time too :)

Solution 2 - C#

Be aware of this when using the answer from Jon Skeet, with code like this:

// 12 days, 23 hours, 24 minutes, 2 seconds.
TimeSpan span = new TimeSpan(12, 23, 24, 2);
// 27 hours, 24 minutes, 2 seconds
TimeSpan span2 = new TimeSpan(27,24,2);

string format = span.ToString("h'h 'm'm 's's'");
string format2 = span2.ToString("h'h 'm'm 's's'");
Console.WriteLine(format);
Console.WriteLine(format2);
Console.ReadLine();

You get results like these:

23h 24m 2s
3h 24m 2s

The hour format can at maximum show 23 hours. It will not show 27 hours or convert the 12 days to hours, it will simply cut them off as if they never existed.

One way to fix this would be to create an extension that checks the length of the TimeSpan and creates formatting based on if the timespan is over a year, day, ect. Or you could simply always show days as well because they never cut off:

string newFormat = span.ToString("d'd 'h'h 'm'm 's's'");

Do note I am a beginner at programming. This is only coming from observations after I was lucky enough to notice this after having assumed it would show all hours. I'm saying this because I don't know if there is a better solution, like another hour format that can display endless hours.

I do however think this format is doing its intended functionality. You just have to be aware of it. Thus this post. Jon Skeet's answer never indicated that this format is to show only the hour property of a date type format where the hours can be at most 23.

Solution 3 - C#

If you're unfortunate enough not to be using .NET4:

string.Format("{0}h{1}m{2}s",
    myTimeSpan.Hours,
    myTimeSpan.Minutes,
    myTimeSpan.Seconds);

Solution 4 - C#

Using Xamarin and .NET Portable 4.5 this was the only format I got working after trying all other answers here:

timespan.ToString("hh':'mm':'ss");

Essentially I'm putting the : in single quotes to escape them.

Solution 5 - C#

This little code sample should help you to parse and reverse-parse TimeSpan:

var t = TimeSpan.FromMilliseconds(450780);
double d1 = t.TotalSeconds;
string t3 = t.ToString(@"hh\:mm\:ss\.f",null);
var tt = TimeSpan.ParseExact(t3, @"hh\:mm\:ss\.f",null);
double d2 = tt.TotalSeconds;

Reference: Custom TimeSpan format strings

Solution 6 - C#

how about concactenation:

String oTime = myTimeSpan.ToString("h") + "h " +
               myTimeSpan.ToString("m") + "m " +
               myTimeSpan.ToString("s") + "s "

UPDATE 1:

You can escape it with single quote: h'h 'm'm 's's'

Solution 7 - C#

I sometimes find format strings a little like regexes, in that when I come back to the code later, I've forgotten the nuances. I've therefore gone with the following, which is less likely to send me back the documentation:

string FormatTimeSpan(TimeSpan timeSpan) =>
    $"{Math.Floor(timeSpan.TotalHours)}h {timeSpan.Minutes}m {timeSpan.Seconds}s";

String interpolation, introduced in C# 6, is making this a lot clearer than it would otherwise be.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAximiliView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#DimensionView Answer on Stackoverflow
Solution 3 - C#HolfView Answer on Stackoverflow
Solution 4 - C#Jonas StensvedView Answer on Stackoverflow
Solution 5 - C#zamoldarView Answer on Stackoverflow
Solution 6 - C#John WooView Answer on Stackoverflow
Solution 7 - C#GilesView Answer on Stackoverflow