16

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.

enter image description here Any clue?

NoWar
  • 36,338
  • 80
  • 323
  • 498

3 Answers3

40

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);
ChrisW
  • 9,151
  • 1
  • 20
  • 34
ManoDestra
  • 6,325
  • 6
  • 26
  • 50
  • 3
    So much better than topvoted/accepted answer – Viktor Mellgren May 16 '19 at 11:26
  • 6
    This 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)` – Faisal Rashid Nov 21 '19 at 05:32
21

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();
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
4

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");
  }
}
Ramazan Kürkan
  • 156
  • 1
  • 10