2016-07-09 2 views
3

Je voudrais convertirTimeSpan à une chaîne personnalisée comme HH: mm: ss

var delta = TimeSpan.FromSeconds(10); 

à chaîne comme 00:00:01

J'essaie cette delta.ToString(@"0:\\hh\\:mm\\:ss", System.Globalization.CultureInfo.InvariantCulture);

Mais rien ne fonctionne bien. Aussi, je ne peux pas trouver ici https://msdn.microsoft.com/en-us/library/ee372287.aspx façon correcte de le faire.

La méthode ToString() ne m'aide pas, donc j'ai besoin dans le format hh:mm:ss.

enter image description here Une idée?

+0

Vous voulez dire * à chaîne comme 00: 00: 10 *? – user3185569

+0

Essayez: var delta = TimeSpan.FromSeconds (10) .ToString ("c"); Voir la page Web: https://msdn.microsoft.com/fr-fr/library/dd992632(v=vs.110).aspx – jdweng

Répondre

4

Il suffit d'utiliser:

delta.ToString(); // 00:00:10 

qui fait l'affaire pour vous (Fiddle)

Ou essayez ceci:

var str = string.Format("{0:00}:{1:00}:{2:00}", delta.Hours, delta.Minutes, delta.Seconds); 

Vous pouvez construire une méthode d'extension si vous l'utilisez beaucoup :

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); 
    } 
} 

Utilisation:

string strSpan = delta.ToCustomString(); 
+0

Non, ce n'est pas il donne des secondes avec des millisecondes comme 00: 08: 59.4199537 –

+0

Il travaille sur ma version .NET. – user3185569

+0

Eh bien j'utilise. Net 4.5 et renvoie 00: 08: 59.4199537 –

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. 

Aussi, si vous voulez un fait sur mesure,

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"); 
    } 
}