Converts TimeSpan to string with ToString() using the specified format: "c", "g", "G" : TimeSpan Format « Date Time « C# / C Sharp






Converts TimeSpan to string with ToString() using the specified format: "c", "g", "G"

   

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" };
      foreach (TimeSpan span in spans)
      {
         foreach (string fmt in fmts)
            Console.WriteLine("{0}: {1}", fmt, span.ToString(fmt));

         Console.WriteLine();
      }
   }
}

   
    
    
  








Related examples in the same category

1.TimeSpan format: %d
2.TimeSpan format: d\.hh\:mm\:ss
3.TimeSpan format: d\.hh\:mm\:ss for new TimeSpan(3, 4, 3, 17)
4.TimeSpan format: h(hour) and m(minutes)
5.Indicate format for TimeSpan parse
6.Standard TimeSpan Format String: c
7.Standard TimeSpan format: g
8.Standard TimeSpan format: G (2)
9.Converts TimeSpan to string using the specified format and culture-specific formatting information.
10.Converts TimeSpan to string with default ToString
11.TimeSpan Format with Flag
12.Convert To TimeSpan