TimeSpan.ToString () returns a string of the type (d: hh: mm: ss)

TimeSpan Ts = new TimeSpan(5, 4, 3, 2);
return Ts.ToString("?");

Which expression should be replaced with a question mark to get this format: 5d: 4h: 3m: 2s ?

+5
source share
3 answers
TimeSpan timeSpan = new TimeSpan(5, 4, 3, 2);
string str = timeSpan.ToString(@"d\d\:h\h\:m\m\:s\s", System.Globalization.CultureInfo.InvariantCulture);

See Custom TimeSpan format strings for how to format TimeSpans.

Although note that the negative TimeSpancannot be distinguished from the positive. They look as if they were canceled . For this, it -new TimeSpan(5,4,3,2)will be displayed as 5d:4h:3m:2s. If you want to display negative numbers, you must format your own numbers, but properties TimeSpan.

+11
source

,

TimeSpan Ts = new TimeSpan(5, 4, 3, 2);
var RetValue = string.Format("{0}d:{1}h:{2}m:{3}s",
    Ts.Days,
    Ts.Hours,
    Ts.Minutes,
    Ts.Seconds);

"5d:4h:0m:2s"

+2
+2
source

All Articles