TimeSpan.ToString Méthode

Définition

Convertit la valeur de l'objet TimeSpan actif en sa représentation équivalente sous forme de chaîne.

Surcharges

ToString(String, IFormatProvider)

Convertit la valeur de l'objet TimeSpan actuel dans sa représentation sous forme de chaîne équivalente à l'aide du format et des informations de mise en forme spécifiques à la culture spécifiés.

ToString(String)

Convertit la valeur de l'objet TimeSpan actuel dans sa représentation sous forme de chaîne équivalente en utilisant le format spécifié.

ToString()

Convertit la valeur de l'objet TimeSpan actif en sa représentation équivalente sous forme de chaîne.

ToString(String, IFormatProvider)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

Convertit la valeur de l'objet TimeSpan actuel dans sa représentation sous forme de chaîne équivalente à l'aide du format et des informations de mise en forme spécifiques à la culture spécifiés.

public:
 virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ formatProvider);
public string ToString (string format, IFormatProvider formatProvider);
public string ToString (string? format, IFormatProvider? formatProvider);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, formatProvider As IFormatProvider) As String

Paramètres

format
String

Chaîne de format TimeSpan standard ou personnalisé.

formatProvider
IFormatProvider

Objet qui fournit des informations de mise en forme propres à la culture.

Retours

Représentation sous forme de chaîne de la valeur actuelle de TimeSpan, tel que spécifié par format et formatProvider.

Implémente

Exceptions

Le paramètre format n’est pas reconnu ou n’est pas pris en charge.

Exemples

L’exemple suivant appelle la ToString(String, IFormatProvider) méthode pour mettre en forme deux intervalles de temps. L’exemple appelle la méthode deux fois pour chaque chaîne de format, d’abord pour l’afficher à l’aide des conventions de la culture en-US, puis pour l’afficher à l’aide des conventions de la culture fr-FR.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      TimeSpan[] intervals = { new TimeSpan(38, 30, 15), 
                               new TimeSpan(16, 14, 30) }; 
      CultureInfo[] cultures = { new CultureInfo("en-US"), 
                                 new CultureInfo("fr-FR") };
      string[] formats = {"c", "g", "G", @"hh\:mm\:ss" };
      Console.WriteLine("{0,12}      Format  {1,22}  {2,22}\n", 
                        "Interval", cultures[0].Name, cultures[1].Name);

      foreach (var interval in intervals) {
         foreach (var fmt in formats)
            Console.WriteLine("{0,12}  {1,10}  {2,22}  {3,22}", 
                              interval, fmt, 
                              interval.ToString(fmt, cultures[0]), 
                              interval.ToString(fmt, cultures[1]));
         Console.WriteLine();
      }  
   }
}
// The example displays the following output:
//        Interval      Format                   en-US                   fr-FR
//    
//      1.14:30:15           c              1.14:30:15              1.14:30:15
//      1.14:30:15           g              1:14:30:15              1:14:30:15
//      1.14:30:15           G      1:14:30:15.0000000      1:14:30:15,0000000
//      1.14:30:15  hh\:mm\:ss                14:30:15                14:30:15
//    
//        16:14:30           c                16:14:30                16:14:30
//        16:14:30           g                16:14:30                16:14:30
//        16:14:30           G      0:16:14:30.0000000      0:16:14:30,0000000
//        16:14:30  hh\:mm\:ss                16:14:30                16:14:30
open System
open System.Globalization

let intervals = 
    [| TimeSpan(38, 30, 15)
       TimeSpan(16, 14, 30) |] 
let cultures = 
    [| CultureInfo "en-US"
       CultureInfo "fr-FR" |]
let formats = [| "c"; "g"; "G"; @"hh\:mm\:ss" |]
printfn $"""{"Interval",12}      Format  {cultures[0].Name,22}  {cultures[1].Name,22}\n""" 

for interval in intervals do
    for fmt in formats do
        printfn $"{interval,12}  {fmt,10}  {interval.ToString(fmt, cultures[0]),22}  {interval.ToString(fmt, cultures[1]),22}"
    printfn ""
// The example displays the following output:
//        Interval      Format                   en-US                   fr-FR
//    
//      1.14:30:15           c              1.14:30:15              1.14:30:15
//      1.14:30:15           g              1:14:30:15              1:14:30:15
//      1.14:30:15           G      1:14:30:15.0000000      1:14:30:15,0000000
//      1.14:30:15  hh\:mm\:ss                14:30:15                14:30:15
//    
//        16:14:30           c                16:14:30                16:14:30
//        16:14:30           g                16:14:30                16:14:30
//        16:14:30           G      0:16:14:30.0000000      0:16:14:30,0000000
//        16:14:30  hh\:mm\:ss                16:14:30                16:14:30
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim intervals() As TimeSpan = { New TimeSpan(38, 30, 15), 
                                      New TimeSpan(16, 14, 30) } 
      Dim cultures() As CultureInfo = { New CultureInfo("en-US"), 
                                        New CultureInfo("fr-FR") }
      Dim formats() As String = {"c", "g", "G", "hh\:mm\:ss" }
      Console.WriteLine("{0,12}      Format  {1,22}  {2,22}", 
                        "Interval", cultures(0).Name, cultures(1).Name)
      Console.WriteLine()
      For Each interval In intervals
         For Each fmt In formats
            Console.WriteLine("{0,12}  {1,10}  {2,22}  {3,22}", 
                              interval, fmt, 
                              interval.ToString(fmt, cultures(0)), 
                              interval.ToString(fmt, cultures(1)))
         Next
         Console.WriteLine()
      Next                                                                                                                                            
   End Sub
End Module
' The example displays the following output:
'        Interval      Format                   en-US                   fr-FR
'    
'      1.14:30:15           c              1.14:30:15              1.14:30:15
'      1.14:30:15           g              1:14:30:15              1:14:30:15
'      1.14:30:15           G      1:14:30:15.0000000      1:14:30:15,0000000
'      1.14:30:15  hh\:mm\:ss                14:30:15                14:30:15
'    
'        16:14:30           c                16:14:30                16:14:30
'        16:14:30           g                16:14:30                16:14:30
'        16:14:30           G      0:16:14:30.0000000      0:16:14:30,0000000
'        16:14:30  hh\:mm\:ss                16:14:30                16:14:30

Remarques

Le format paramètre peut être n’importe quel spécificateur de format standard ou personnalisé valide pour les TimeSpan valeurs. Si format est égal à String.Empty ou est null, la valeur de retour de l’objet actif TimeSpan est mise en forme avec le spécificateur de format commun (« c »). Si format est une autre valeur, la méthode lève un FormatException.

Important

Les chaînes de format personnalisées pour TimeSpan les valeurs n’incluent pas de séparateur de date ou d’heure. Si vous souhaitez inclure ces éléments dans votre chaîne de format, vous devez les traiter comme des littéraux de caractères. Pour plus d’informations, consultez l’exemple d’illustration et la rubrique Chaînes de format TimeSpan personnalisées .

.NET fournit une prise en charge étendue de la mise en forme, qui est décrite plus en détail dans les rubriques de mise en forme suivantes :

Le formatProvider paramètre est une IFormatProvider implémentation qui fournit des informations spécifiques à la culture sur le format de la chaîne retournée. Le formatProvider paramètre peut être l’un des éléments suivants :

Si formatProvider est null, l’objet DateTimeFormatInfo associé à la culture actuelle est utilisé. Si format est une chaîne de format personnalisée, le formatProvider paramètre est ignoré.

Voir aussi

S’applique à

ToString(String)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

Convertit la valeur de l'objet TimeSpan actuel dans sa représentation sous forme de chaîne équivalente en utilisant le format spécifié.

public:
 System::String ^ ToString(System::String ^ format);
public string ToString (string format);
public string ToString (string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

Paramètres

format
String

Chaîne de format TimeSpan standard ou personnalisé.

Retours

Représentation sous forme de chaîne de la valeur actuelle de TimeSpan au format spécifié par le paramètre format.

Exceptions

Le paramètre format n’est pas reconnu ou n’est pas pris en charge.

Exemples

L’exemple suivant utilise des chaînes de format standard et personnalisées TimeSpan pour afficher la représentation sous forme de chaîne de chaque élément dans un tableau de TimeSpan valeurs

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.
let spans = 
    [| TimeSpan.Zero
       TimeSpan(-14, 0, 0, 0, 0)
       TimeSpan(1, 2, 3)
       TimeSpan(0, 0, 0, 0, 250)
       TimeSpan(99, 23, 59, 59, 999)
       TimeSpan(3, 0, 0)
       TimeSpan(0, 0, 0, 0, 25) |]


let fmts = [| "c"; "g"; "G"; @"hh\:mm\:ss"; "%m' min.'" |]
for span in spans do
    for fmt in fmts do
        printfn $"{fmt}: {span.ToString fmt}"
    printfn ""
// 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.
Module Example
   Public Sub Main()
      Dim spans() As TimeSpan = { 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) }
      Dim fmts() As String = { "c", "g", "G", "hh\:mm\:ss", "%m' min.'" }
      For Each span As TimeSpan In spans
         For Each fmt As String In fmts
            Console.WriteLine("{0}: {1}", fmt, span.ToString(fmt))
         Next
         Console.WriteLine()         
      Next
   End Sub
End Module
' 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.

Remarques

Le format paramètre peut être n’importe quel spécificateur de format standard ou personnalisé valide pour les TimeSpan valeurs. Si format est égal à String.Empty ou est null, la valeur de retour de l’objet actif TimeSpan est mise en forme avec le spécificateur de format commun (« c »). Si format est une autre valeur, la méthode lève un FormatException.

Si format est une chaîne de format standard, le format de la chaîne retournée est défini par les conventions de mise en forme de la culture actuelle.

Important

Les chaînes de format personnalisées pour TimeSpan les valeurs n’incluent pas de séparateur de date ou d’heure. Si vous souhaitez inclure ces éléments dans votre chaîne de format, vous devez les traiter comme des littéraux de caractères. Pour plus d’informations, consultez l’exemple d’illustration et la rubrique Chaînes de format TimeSpan personnalisées .

.NET fournit une prise en charge étendue de la mise en forme, qui est décrite plus en détail dans les rubriques de mise en forme suivantes :

Voir aussi

S’applique à

ToString()

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

Convertit la valeur de l'objet TimeSpan actif en sa représentation équivalente sous forme de chaîne.

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

Retours

Représentation sous forme de chaîne de la valeur actuelle de TimeSpan.

Exemples

L’exemple suivant affiche les chaînes retournées en appelant la ToString méthode avec un nombre de TimeSpan valeurs. Notez que bien que l’exemple n’appelle pas directement la ToString méthode, elle est appelée par la Console.WriteLine méthode lorsqu’elle tente de convertir une TimeSpan valeur en sa représentation sous forme de chaîne.

 TimeSpan span;
 
 // Initialize a time span to zero.
 span = TimeSpan.Zero;
 Console.WriteLine(span);

 // Initialize a time span to 14 days.
 span = new TimeSpan(-14, 0, 0, 0, 0);
 Console.WriteLine(span);

 // Initialize a time span to 1:02:03.
 span = new TimeSpan(1, 2, 3);
 Console.WriteLine(span);

 // Initialize a time span to 250 milliseconds.
 span = new TimeSpan(0, 0, 0, 0, 250);
 Console.WriteLine(span);
 
 // Initialize a time span to 99 days, 23 hours, 59 minutes, and 59.999 seconds.
 span = new TimeSpan(99, 23, 59, 59, 999);
 Console.WriteLine(span);
 
 // Initialize a time span to 3 hours.
 span = new TimeSpan(3, 0, 0);
 Console.WriteLine(span);
 
 // Initialize a timespan to 25 milliseconds.
 span = new TimeSpan(0, 0, 0, 0, 25);
 Console.WriteLine(span);

 // The example displays the following output:
 //       00:00:00
 //       -14.00:00:00
 //       01:02:03
 //       00:00:00.2500000
 //       99.23:59:59.9990000
 //       03:00:00
 //       00:00:00.0250000
// Initialize a time span to zero.
let span = TimeSpan.Zero
printfn $"{span}"

// Initialize a time span to 14 days.
let span = TimeSpan(-14, 0, 0, 0, 0)
printfn $"{span}"

// Initialize a time span to 1:02:03.
let span = TimeSpan(1, 2, 3)
printfn $"{span}"

// Initialize a time span to 250 milliseconds.
let span = TimeSpan(0, 0, 0, 0, 250)
printfn $"{span}"

// Initialize a time span to 99 days, 23 hours, 59 minutes, and 59.999 seconds.
let span = TimeSpan(99, 23, 59, 59, 999)
printfn $"{span}"

// Initialize a time span to 3 hours.
let span = TimeSpan(3, 0, 0)
printfn $"{span}"

// Initialize a timespan to 25 milliseconds.
let span = TimeSpan(0, 0, 0, 0, 25)
printfn $"{span}"

// The example displays the following output:
//       00:00:00
//       -14.00:00:00
//       01:02:03
//       00:00:00.2500000
//       99.23:59:59.9990000
//       03:00:00
//       00:00:00.0250000
Module ToString
   Public Sub Main()
      Dim span As TimeSpan
      
      ' Initialize a time span to zero.
      span = TimeSpan.Zero
      Console.WriteLine(span)
      
      ' Initialize a time span to 14 days.
      span = New TimeSpan(-14, 0, 0, 0, 0)
      Console.WriteLine(span)
     
      ' Initialize a time span to 1:02:03.
      span = New TimeSpan(1, 2, 3)
      Console.WriteLine(span)
      
      
      ' Initialize a time span to 250 milliseconds.
      span = New TimeSpan(0, 0, 0, 0, 250)
      Console.WriteLine(span)
      
      ' Initialize a time span to 99 days, 23 hours, 59 minutes, and 59.9999999 seconds.
      span = New TimeSpan(99, 23, 59, 59, 999)
      Console.WriteLine(span)
      
      ' Initialize a time span to 3 hours.
      span = New TimeSpan(3, 0, 0)
      Console.WriteLine(span)
      
      ' Initialize a timespan to 25 milliseconds.
      span = New TimeSpan(0, 0, 0, 0, 25)
      Console.WriteLine(span)
   End Sub
End Module
' The example displays the following output:
'       00:00:00
'       -14.00:00:00
'       01:02:03
'       00:00:00.2500000
'       99.23:59:59.9990000
'       03:00:00
'       00:00:00.0250000

Remarques

La chaîne retournée est mise en forme avec le spécificateur de format « c » et a le format suivant :

[-][d.]hh:mm:ss[.fffffff]

Les éléments entre crochets ([ et ]) peuvent ne pas être inclus dans la chaîne retournée. Les points-virgules et les points (: et.) sont des caractères littérals. Les éléments non littérals sont répertoriés dans le tableau suivant. Notez que la chaîne retournée par la ToString() méthode n’est pas sensible à la culture.

Élément Description
"-" Signe moins, qui indique un intervalle de temps négatif. Aucun signe n’est inclus pour un intervalle de temps positif.
« d » Nombre de jours dans l’intervalle de temps. Cet élément est omis si l’intervalle de temps est inférieur à un jour.
« hh » Nombre d’heures dans l’intervalle de temps, compris entre 0 et 23.
« mm » Nombre de minutes dans l’intervalle de temps, compris entre 0 et 59.
« ss » Nombre de secondes dans l’intervalle de temps, compris entre 0 et 59.
« fffffff » Fractions de secondes dans l’intervalle de temps. Cet élément est omis si l’intervalle de temps n’inclut pas de fractions de secondes. Le cas échéant, les fractions de secondes sont toujours exprimées à l’aide de sept chiffres décimaux.

Notes pour les appelants

La prise en charge de la mise en forme des TimeSpan valeurs a été ajoutée dans .NET Framework 4. Toutefois, la surcharge de ToString() méthode reste insensible à la culture. Son comportement reste inchangé par rapport aux versions précédentes du .NET Framework. Pour contrôler la mise en forme d’une TimeSpan valeur, appelez la ToString(String) surcharge ou ToString(String, IFormatProvider) .

Voir aussi

S’applique à