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

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");
  }
}
Share:
28,898
NoWar
Author by

NoWar

[email protected]

Updated on October 04, 2020

Comments

  • NoWar
    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 lie hh:mm:ss.

    enter image description here Any clue?

  • NoWar
    NoWar almost 8 years
    No it is not it gives seconds with milliseconds like 00:08:59.4199537
  • Zein Makki
    Zein Makki almost 8 years
    It is working on my .NET Version.
  • NoWar
    NoWar almost 8 years
    Well I use .net 4.5 and returns 00:08:59.4199537
  • Zein Makki
    Zein Makki almost 8 years
  • NoWar
    NoWar almost 8 years
    I use Visual Studio 2015 and it works as I see, fiddle is wrong,,,
  • Zein Makki
    Zein Makki almost 8 years
    @Dimi What do you mean fiddle is wrong ? It produces 00:00:10
  • NoWar
    NoWar almost 8 years
    Yes, man I will add a screenshot to my question,
  • Zein Makki
    Zein Makki almost 8 years
    @Dimi i can't see any edit in your question.
  • Zein Makki
    Zein Makki almost 8 years
    @Dimi check my edited answer.
  • NoWar
    NoWar almost 8 years
    Yep, the last code is great!
  • ManoDestra
    ManoDestra almost 8 years
    You 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.
    Mickael V. almost 6 years
    Only 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
    Viktor Mellgren almost 5 years
    So much better than topvoted/accepted answer
  • Faisal Rashid
    Faisal Rashid over 4 years
    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)