TimeSpan to Custom string like HH:mm:ss
28,898
Solution 1
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();
Solution 2
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);
Solution 3
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");
}
}
Comments
-
NoWar over 3 years
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 liehh:mm:ss
. -
NoWar almost 8 yearsNo it is not it gives seconds with milliseconds like 00:08:59.4199537
-
Zein Makki almost 8 yearsIt is working on my .NET Version.
-
NoWar almost 8 yearsWell I use .net 4.5 and returns 00:08:59.4199537
-
Zein Makki almost 8 yearsCheck this: dotnetfiddle.net/NJXc11
-
NoWar almost 8 yearsI use Visual Studio 2015 and it works as I see, fiddle is wrong,,,
-
Zein Makki almost 8 years@Dimi What do you mean fiddle is wrong ? It produces
00:00:10
-
NoWar almost 8 yearsYes, man I will add a screenshot to my question,
-
Zein Makki almost 8 years@Dimi i can't see any edit in your question.
-
Zein Makki almost 8 years@Dimi check my edited answer.
-
NoWar almost 8 yearsYep, the last code is great!
-
ManoDestra almost 8 yearsYou can do the same on a TimeSpan's ToString method. You can pass the format in the ToString() method of the TimeSpan itself. No need to use string.format(). Example in my answer.
-
Mickael V. almost 6 yearsOnly reason the fiddle is right is because the TimeSpan is 10 seconds, round. Try with
TimeSpan.FromMilliseconds(1234567);
and it will show milliseconds. Ramazan has the right answer -
Viktor Mellgren almost 5 yearsSo much better than topvoted/accepted answer
-
Faisal Rashid over 4 yearsThis won't work if the "hh" part exceeds 24. It will simply provide the HOURS part for "hh", i.e., if TimeSpan is 36 hours + something, it will display 12 for "hh". You could achieve the correct output for that case by using the following -
String.Format("{0}:{1}:{2}", Math.Floor(ts.TotalHours), ts.Minutes, ts.Seconds)