I would like to convert
var delta = TimeSpan.FromSeconds(10);
to string like 00:00:01
I try this delta.ToString(@"0:\\hh\\:mm\\:ss", System.Globalization.CultureInfo.InvariantCulture);
But nothing is working fine. Also I cannot find here https://msdn.microsoft.com/en-us/library/ee372287.aspx correct way to do it.
Method ToString()
does not help so i need in format lie hh:mm:ss
.
Any clue?
ToString(String, IFormatProvider) Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format and culture-specific formatting information.
NET Framework 4. "c" is the default TimeSpan format string; the TimeSpan. ToString() method formats a time interval value by using the "c" format string. TimeSpan also supports the "t" and "T" standard format strings, which are identical in behavior to the "c" standard format string.
To convert a DateTime to a TimeSpan you should choose a base date/time - e.g. midnight of January 1st, 2000, and subtract it from your DateTime value (and add it when you want to convert back to DateTime ). If you simply want to convert a DateTime to a number you can use the Ticks property. Save this answer.
C# TimeSpan struct represents a time interval that is difference between two times measured in number of days, hours, minutes, and seconds. C# TimeSpan is used to compare two C# DateTime objects to find the difference between two dates.
Just use :
delta.ToString(); // 00:00:10
which does the trick for you (Fiddle)
Or try this:
var str = string.Format("{0:00}:{1:00}:{2:00}", delta.Hours, delta.Minutes, delta.Seconds);
You can build an extension method if you use it a lot:
public static class MyExtensions
{
public static string ToCustomString(this TimeSpan span)
{
return string.Format("{0:00}:{1:00}:{2:00}", span.Hours, span.Minutes, span.Seconds);
}
}
Usage:
string strSpan = delta.ToCustomString();
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