How can I String.Format a TimeSpan object with a custom format in .NET?

timespan tostring format
c# timespan tostring format hours and minutes
convert timespan to 12 hour format in c#
c# timespan format milliseconds
c# timespan format am/pm
convert timespan to string with format
c# timespan to string hours and minutes
c# timespan format without milliseconds

What is the recommended way of formatting TimeSpan objects into a string with a custom format?

Please note: this answer is for .Net 4.0 and above. If you want to format a TimeSpan in .Net 3.5 or below please see JohannesH's answer.

Custom TimeSpan format strings were introduced in .Net 4.0. You can find a full reference of available format specifiers at the MSDN Custom TimeSpan Format Strings page.

Here's an example timespan format string:

string.Format("{0:hh\\:mm\\:ss}", myTimeSpan); //example output 15:36:15

(UPDATE) and here is an example using C# 6 string interpolation:

$"{myTimeSpan:hh\\:mm\\:ss}"; //example output 15:36:15

You need to escape the ":" character with a "\" (which itself must be escaped unless you're using a verbatim string).

This excerpt from the MSDN Custom TimeSpan Format Strings page explains about escaping the ":" and "." characters in a format string:

The custom TimeSpan format specifiers do not include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals. For example, "dd.hh:mm" defines a period (.) as the separator between days and hours, and a colon (:) as the separator between hours and minutes.

Standard TimeSpan format strings, What is the recommended way of formatting TimeSpan objects into a string with a custom format? Please note: this answer is for .Net 4.0 and above. If you want  Custom TimeSpan format strings were introduced in .Net 4.0. You can find a full reference of available format specifiers at the MSDN Custom TimeSpan Format Strings page. Here's an example timespan format string: string.Format("{0:hh\\:mm\\:ss}", myTimeSpan); //example output 15:36:15 (UPDATE) and here is an example using C# 6 string interpolation:

For .NET 3.5 and lower you could use:

string.Format ("{0:00}:{1:00}:{2:00}", 
               (int)myTimeSpan.TotalHours, 
                    myTimeSpan.Minutes, 
                    myTimeSpan.Seconds);

Code taken from a Jon Skeet answer on bytes

For .NET 4.0 and above, see DoctaJonez answer.

How can I String.Format a TimeSpan object with a custom format in , To do formatting of a TimeSpan object, use TimeSpan.ToString method. Check out the Microsoft Documentation on TimeSpan custom format strings for more  The custom TimeSpan format specifiers don't include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals.

One way is to create a DateTime object and use it for formatting:

new DateTime(myTimeSpan.Ticks).ToString(myCustomFormat)

// or using String.Format:
String.Format("{0:HHmmss}", new DateTime(myTimeSpan.Ticks))

This is the way I know. I hope someone can suggest a better way.

How can I String.Format a TimeSpan object with a custom - iOS, format strings. formatting [.NET Framework], time intervals For more information, see Custom TimeSpan format strings . The string TimeSpan objects, uses them to perform arithmetic operations, and displays the result. In each case, it  The easiest way to format a TimeSpan is to add it to a DateTime and format that: string formatted = (DateTime.Today + dateDifference).ToString("HH 'hrs' mm 'mins' ss 'secs'"); This works as long as the time difference is not more than 24 hours.

Simple. Use TimeSpan.ToString with c, g or G. More information at MSDN

Convert a TimeSpan to a formatted string in C#, A custom format string consists of one or more custom date and time format DateTimeOffset thisDate2 = new DateTimeOffset(2011, 6, 10, 15, 24, 16, TimeSpan. current culture's CultureInfo object to use a GregorianCalendar object whose  Please note: this answer is for .Net 4.0 and above. If you want to format a TimeSpan in .Net 3.5 or below please see JohannesH's answer. Custom TimeSpan format strings were introduced in .Net 4.0. You can find a full reference of available format specifiers at the MSDN Custom TimeSpan Format Strings page. Here's an example timespan format string:

I would go with

myTimeSpan.ToString("hh\\:mm\\:ss");

docs/standard-timespan-format-strings.md at master · dotnet/docs , Exploring custom format strings for TimeSpan in PowerShell and C#. NET, or can be made a little easier by using native PowerShell  The format parameter can be any valid standard or custom format specifier for TimeSpan values. If format is equal to String.Empty or is null, the return value of the current TimeSpan object is formatted with the common format specifier ("c"). If format is any other value, the method throws a FormatException.

Custom Date and Time Format Strings, Custom TimeSpan Format Strings : TimeSpan « Date Time « C# / C Sharp. Returns a new TimeSpan object whose value is the absolute value of the current​  Standard TimeSpan Format Strings. A standard TimeSpan format string uses a single format specifier to define the text representation of a TimeSpan value that results from a formatting operation. Any format string that contains more than one character, including white space, is interpreted as a custom TimeSpan format string.

Formatting TimeSpan in PowerShell, Format but there is very little explanation of its usage in the mono docs. I prefer using TimeSpan (namespace System) in c#. /en-us/dotnet/api/system.string.​format?view=netframework-4.7.2#the-format-item NET are the same. If you have a DateTime you can use custom format strings as explained  The value of a TimeSpan object is the number of ticks that equal the represented time interval. A tick is equal to 100 nanoseconds, or one ten-millionth of a second. The value of a TimeSpan object can range from TimeSpan.MinValue to TimeSpan.MaxValue. Instantiating a TimeSpan Value. You can instantiate a TimeSpan value in a number of ways:

Custom TimeSpan Format Strings : TimeSpan « Date Time « C# / C , Format string. A TimeSpan can be formatted to a string with a format string. We can use codes like hh, mm and ss. We often must escape the ":" chars. Here: We create a TimeSpan of 3 hours, 30 minutes. We format it with an hours: minutes: seconds format. ToString: We can use these format strings in ToString, Console.WriteLine and string.Format.

Comments
  • @Andrei Rinea: Correct, as stated at the start of my second paragraph ".Net 4 allows you to use custom format strings with Timespan".
  • @Edward, that's not quite right. In your example you're escaping the first m and the first s, so with an input of myTimeSpan = new TimeSpan(15, 35, 54); the statement myTimeSpan .ToString("hh\\mm\\ss"); will result in 15m35s54. I don't think that's what you intended as it'll place an m after your hours and an s after your minutes.
  • @Doctor Jones - Thank you! I meant myTimeSpan.ToString("h\\hm\\ms\\s"); or myTimeSpan.ToString(@"h\hm\ms\s"); which gives 15h35m54s
  • just be careful with this solution, because it will not work correctly when the Hours part is more than 24
  • @QuarK, there is no custom format specifier that does that, they all give you the hours that are not counted as part of days. You could do this instead though $"{myTimeSpan.TotalHours}:{myTimeSpan:mm\\:ss}". From a user point of view, it might be better to output the days though, nobody wants to mentally figure out how many days are in 200+ hours.
  • Yes, thank you. But I think that DateTime approach is more customizable, as it would work for any time format supported by DateTime. This approach is hard to use for showing AM/PM for example.
  • Sure, TimeSpan is meant to represents a period of time, not a time of day (Even though the DateTime.Now.TimeOfDay property would have you believe otherwise). If you need to represent a specific time of day I suggest you continue using the DateTime class.
  • Just remember that if the TimeSpan is equal to or more than 24 hours you will get incorrect formatting.
  • This is really only going to work if the TimeSpan is less than a day. That might not be a such a terrible restriction, but it keeps it from being a general solution.
  • Does it return correct value ? Dim ts As New TimeSpan(11, 22, 30, 30):Dim sss As String = New DateTime(ts.Ticks).ToString("dd.hh:mm:ss")
  • Thank you for your answer. This method is apparently new in .NET 4, and did not exist when the question was asked. It also does not support custom formats. Nevertheless, it's a valuable addition to the answers to this questions. Thanks again.
  • Simple and clean! an alternative is @"hh\:mm\:ss"
  • You need to cast the myTimeSpan.TotalHours to an int - otherwise it might get rounded-up. See JohannesH's answer