TimeSpan to Custom string like HH:mm:ss
Just use the ToString(String format) method of TimeSpan, passing in the format you require.
https://msdn.microsoft.com/en-us/library/dd992632(v=vs.110).aspx
E.g.
var ts = TimeSpan.FromMinutes(10000);
var output = ts.ToString(@"hh\:mm\:ss");
Console.WriteLine(output);
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();
From MSDN
using System;
public class Class1
{
public static void Main()
{
TimeSpan[] spans = { TimeSpan.Zero, new TimeSpan(-14, 0, 0, 0, 0),
new TimeSpan(1, 2, 3),
new TimeSpan(0, 0, 0, 0, 250),
new TimeSpan(99, 23, 59, 59, 999),
new TimeSpan(3, 0, 0),
new TimeSpan(0, 0, 0, 0, 25) };
string[] fmts = { "c", "g", "G", @"hh\:mm\:ss", "%m' min.'" };
foreach (TimeSpan span in spans)
{
foreach (string fmt in fmts)
Console.WriteLine("{0}: {1}", fmt, span.ToString(fmt));
Console.WriteLine();
}
}
}
// The example displays the following output:
// c: 00:00:00
// g: 0:00:00
// G: 0:00:00:00.0000000
// hh\:mm\:ss: 00:00:00
// %m' min.': 0 min.
//
// c: -14.00:00:00
// g: -14:0:00:00
// G: -14:00:00:00.0000000
// hh\:mm\:ss: 00:00:00
// %m' min.': 0 min.
//
// c: 01:02:03
// g: 1:02:03
// G: 0:01:02:03.0000000
// hh\:mm\:ss: 01:02:03
// %m' min.': 2 min.
//
// c: 00:00:00.2500000
// g: 0:00:00.25
// G: 0:00:00:00.2500000
// hh\:mm\:ss: 00:00:00
// %m' min.': 0 min.
//
// c: 99.23:59:59.9990000
// g: 99:23:59:59.999
// G: 99:23:59:59.9990000
// hh\:mm\:ss: 23:59:59
// %m' min.': 59 min.
//
// c: 03:00:00
// g: 3:00:00
// G: 0:03:00:00.0000000
// hh\:mm\:ss: 03:00:00
// %m' min.': 0 min.
//
// c: 00:00:00.0250000
// g: 0:00:00.025
// G: 0:00:00:00.0250000
// hh\:mm\:ss: 00:00:00
// %m' min.': 0 min.
Also if you want a custom made,
public static class TimeSpanExt
{
public static string ToStringMyFormat(this TimeSpan timeSpan)
{
return timeSpan.Days.ToString("00") + ":" + timeSpan.Hours.ToString("00") + ":" + timeSpan.Minutes.ToString("00") + ":" + timeSpan.Seconds.ToString("00") + ":" + timeSpan.Milliseconds.ToString("00");
}
}