TimeSpan.ToString 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将当前 TimeSpan 对象的值转换为其等效的字符串表示形式。
重载
| ToString() | 将当前 TimeSpan 对象的值转换为其等效的字符串表示形式。 | 
| ToString(String) | 使用指定的格式将当前 TimeSpan 对象的值转换为其等效的字符串表示形式。 | 
| ToString(String, IFormatProvider) | 使用指定的格式和区域性特定的格式设置信息,将当前 TimeSpan 对象的值转换为其等效字符串表示形式。 | 
ToString()
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
将当前 TimeSpan 对象的值转换为其等效的字符串表示形式。
public:
 override System::String ^ ToString();public override string ToString ();override this.ToString : unit -> stringPublic Overrides Function ToString () As String返回
当前 TimeSpan 值的字符串表示形式。
示例
以下示例显示通过调用 ToString 具有多个值的 方法返回的 TimeSpan 字符串。 请注意,尽管该示例不直接调用 ToString 方法,但它在尝试将值转换为TimeSpan其字符串表示形式时由 Console.WriteLine 方法调用。
 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
注解
返回的字符串使用“c”格式说明符进行格式设置,并具有以下格式:
[-][d.]hh:mm:ss[.fffffff]
返回的字符串中可能不包含方括号 ([ 和 ]) 中的元素。 冒号和句点 (: 和.) 是文本字符。 下表列出了非文本元素。 请注意, 方法返回的 ToString() 字符串不区分区域性。
| 项 | 描述 | 
|---|---|
| "-" | 一个减号,指示负时间间隔。 对于正时间跨度,不包括任何符号。 | 
| “d” | 时间间隔中的天数。 如果时间间隔小于一天,则省略此元素。 | 
| “hh” | 时间间隔中的小时数,范围为 0 到 23。 | 
| “mm” | 时间间隔中的分钟数,范围为 0 到 59。 | 
| “ss” | 时间间隔中的秒数,范围为 0 到 59。 | 
| “fffffff” | 时间间隔中的秒的小数部分。 如果时间间隔不包括秒的小数部分,则省略此元素。 如果存在,则秒的小数部分始终使用七位小数表示。 | 
调用方说明
.NET Framework 4 中添加了对值格式设置TimeSpan的支持。 但是,方法 ToString() 重载仍然不区分区域性。 其行为与以前版本的.NET Framework保持不变。 若要控制值的格式设置 TimeSpan ,请 ToString(String) 调用 或 ToString(String, IFormatProvider) 重载。
另请参阅
适用于
ToString(String)
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
使用指定的格式将当前 TimeSpan 对象的值转换为其等效的字符串表示形式。
public:
 System::String ^ ToString(System::String ^ format);public string ToString (string format);public string ToString (string? format);override this.ToString : string -> stringPublic Function ToString (format As String) As String参数
返回
当前 TimeSpan 值的字符串表示形式,该值使用 format 参数指定的格式。
例外
无法识别或不支持 format 参数。
示例
以下示例使用标准和自定义 TimeSpan 格式字符串来显示值数组中每个元素的 TimeSpan 字符串表示形式
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.
注解
参数 format 可以是值的任何有效标准或自定义格式说明符 TimeSpan 。 如果 format 等于 String.Empty 或 为 null,则使用通用格式说明符 (“c”) 设置当前 TimeSpan 对象的返回值的格式。 如果 format 是任何其他值,该方法将 FormatException引发 。
如果 format 是标准格式字符串,则返回的字符串的格式由当前区域性的格式设置约定定义。
重要
值的自定义格式字符串 TimeSpan 不包括日期或时间分隔符。 如果要将这些元素包含在格式字符串中,则必须将它们视为字符文本。 有关详细信息,请参阅示例以获取插图,并参阅 自定义 TimeSpan 格式字符串 主题。
.NET 提供广泛的格式设置支持,以下格式设置主题对此进行了更详细的介绍:
- 有关值格式字符串 TimeSpan 的详细信息,请参阅 标准 TimeSpan 格式字符串 和 自定义 TimeSpan 格式字符串。 
- 有关 .NET 中对格式设置的支持的详细信息,请参阅 格式设置类型。 
另请参阅
适用于
ToString(String, IFormatProvider)
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
使用指定的格式和区域性特定的格式设置信息,将当前 TimeSpan 对象的值转换为其等效字符串表示形式。
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 -> stringPublic Function ToString (format As String, formatProvider As IFormatProvider) As String参数
- formatProvider
- IFormatProvider
一个提供区域性特定的格式设置信息的对象。
返回
当前 TimeSpan 值的字符串表示形式,由 format 和 formatProvider 指定。
实现
例外
无法识别或不支持 format 参数。
示例
以下示例调用 ToString(String, IFormatProvider) 方法以设置两个时间间隔的格式。 该示例为每个格式字符串调用方法两次,首先使用 en-US 区域性的约定显示该方法,然后使用 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
注解
参数 format 可以是值的任何有效标准或自定义格式说明符 TimeSpan 。 如果 format 等于 String.Empty 或 为 null,则使用通用格式说明符 (“c”) 设置当前 TimeSpan 对象的返回值的格式。 如果 format 为任何其他值,该方法将 FormatException引发 。
重要
值的自定义格式字符串 TimeSpan 不包括日期或时间分隔符。 如果要将这些元素包含在格式字符串中,则必须将它们视为字符文本。 有关详细信息,请参阅示例以获取插图,并参阅 自定义 TimeSpan 格式字符串 主题。
.NET 提供广泛的格式设置支持,以下格式设置主题对此进行了更详细的介绍:
- 有关值格式字符串 TimeSpan 的详细信息,请参阅 标准 TimeSpan 格式字符串 和 自定义 TimeSpan 格式字符串。 
- 有关 .NET 中对格式设置的支持的详细信息,请参阅 格式设置类型。 
参数 formatProvider 是一个 IFormatProvider 实现,它提供有关返回字符串的格式的区域性特定信息。 参数 formatProvider 可以是以下任一项:
- 一个 CultureInfo 对象,表示其格式约定将反映在返回的字符串中的区域性。 DateTimeFormatInfo属性CultureInfo.DateTimeFormat返回的对象定义返回的字符串的格式。 
- 一个 DateTimeFormatInfo 对象,该对象定义返回的字符串的格式设置。 
- 实现 接口的 IFormatProvider 自定义对象。 其 IFormatProvider.GetFormat 方法返回一个 DateTimeFormatInfo 对象,该对象提供格式设置信息。 
如果 formatProvider 为 null,则 DateTimeFormatInfo 使用与当前区域性关联的 对象。 如果 format 是自定义格式字符串,则 formatProvider 忽略 参数。