How convert TimeSpan to 24 hours and minutes String?

Majid picture Majid · Aug 28, 2013 · Viewed 50.6k times · Source

I use this code for converting Timespan to String (for ex: 14:53) :

myTimeSpan.ToString("hh:mm");

but this error occurs:

Input string was not in a correct format

What is the proper way to do this?

Answer

Tim Schmelter picture Tim Schmelter · Aug 28, 2013
myTimeSpan.ToString(@"hh\:mm")

Custom TimeSpan Format Strings

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.