TimeSpan Ts = new TimeSpan(5, 4, 3, 2);
return Ts.ToString("?");
What expression should I replace with a question mark to get this format: 5d:4h:3m:2s ?
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 on how to format TimeSpan
s.
Though note that negative TimeSpan
s cannot be distinguished from positive ones. They appear like they have been negated. Therefor -new TimeSpan(5,4,3,2)
will still show as 5d:4h:3m:2s
. If you want negative numbers to display, you should format your own numbers though the properties of TimeSpan
.
You can accomplish this by using your current code
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);
yields this as a formatted result "5d:4h:0m:2s"
This works for me
"d'd:'h'h:'m'm:'s's'"
Found here http://msdn.microsoft.com/en-us/library/ee372287.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With