TimeSpan.ToString 方法

定义

将当前 TimeSpan 对象的值转换为其等效的字符串表示形式。Converts the value of the current TimeSpan object to its equivalent string representation.

重载

ToString(String, IFormatProvider)

使用指定的格式和区域性特定的格式设置信息,将当前 TimeSpan 对象的值转换为其等效字符串表示形式。Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format and culture-specific formatting information.

ToString(String)

使用指定的格式将当前 TimeSpan 对象的值转换为其等效的字符串表示形式。Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format.

ToString()

将当前 TimeSpan 对象的值转换为其等效的字符串表示形式。Converts the value of the current TimeSpan object to its equivalent string representation.

ToString(String, IFormatProvider)

使用指定的格式和区域性特定的格式设置信息,将当前 TimeSpan 对象的值转换为其等效字符串表示形式。Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format and culture-specific formatting information.

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

参数

format
String

标准或自定义的 TimeSpan 格式字符串。A standard or custom TimeSpan format string.

formatProvider
IFormatProvider

一个提供区域性特定的格式设置信息的对象。An object that supplies culture-specific formatting information.

返回

String

当前 TimeSpan 值的字符串表示形式,由 formatformatProvider 指定。The string representation of the current TimeSpan value, as specified by format and formatProvider.

实现

例外

无法识别或不支持 format 参数。The format parameter is not recognized or is not supported.

示例

下面的示例调用 ToString(String, IFormatProvider) 方法以设置两个时间间隔的格式。The following example calls the ToString(String, IFormatProvider) method to format two time intervals. 该示例对每个格式字符串调用两次方法,首先使用 en-us 区域性的约定显示该方法,然后使用 fr-fr 区域性的约定显示该方法。The example calls the method twice for each format string, first to display it using the conventions of the en-US culture and then to display it using the conventions of the fr-FR culture.

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
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参数可以是任何有效的标准或自定义格式说明符 TimeSpanThe format parameter can be any valid standard or custom format specifier for TimeSpan values. 如果 format 等于 String.Empty 或为,则 null 当前对象的返回值 TimeSpan 用通用格式说明符格式 ( "c" ) 。If format is equal to String.Empty or is null, the return value of the current TimeSpan object is formatted with the common format specifier ("c"). 如果 format 为任何其他值,则该方法将引发 FormatExceptionIf format is any other value, the method throws a FormatException.

重要

值的自定义格式字符串 TimeSpan 不包含日期或时间分隔符。The custom format strings for TimeSpan values do not include a date or time separator. 如果要在格式字符串中包含这些元素,则必须将它们视为字符文本。If you want to include these elements in your format string, you must treat them as character literals. 有关详细信息,请参阅示例,并查看 自定义的 TimeSpan 格式字符串 主题。See the example for an illustration, and see the Custom TimeSpan Format Strings topic for more information.

.NET 提供广泛的格式支持,以下格式设置主题对此进行了更详细的介绍:.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

formatProvider参数是一个 IFormatProvider 实现,提供有关返回的字符串格式的区域性特定信息。The formatProvider parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string. formatProvider参数可以是以下任一项:The formatProvider parameter can be any of the following:

如果 formatProvidernull ,则 DateTimeFormatInfo 使用与当前区域性关联的对象。If formatProvider is null, the DateTimeFormatInfo object that is associated with the current culture is used. 如果 format 是一个自定义格式字符串,则 formatProvider 忽略该参数。If format is a custom format string, the formatProvider parameter is ignored.

适用于

ToString(String)

使用指定的格式将当前 TimeSpan 对象的值转换为其等效的字符串表示形式。Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format.

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

参数

format
String

标准或自定义的 TimeSpan 格式字符串。A standard or custom TimeSpan format string.

返回

String

当前 TimeSpan 值的字符串表示形式,该值使用 format 参数指定的格式。The string representation of the current TimeSpan value in the format specified by the format parameter.

例外

无法识别或不支持 format 参数。The format parameter is not recognized or is not supported.

示例

下面的示例使用标准和自定义 TimeSpan 格式字符串来显示值数组中每个元素的字符串表示形式。 TimeSpanThe following example uses standard and custom TimeSpan format strings to display the string representation of each element in an array of TimeSpan values

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.
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参数可以是任何有效的标准或自定义格式说明符 TimeSpanThe format parameter can be any valid standard or custom format specifier for TimeSpan values. 如果 format 等于 String.Empty 或为,则 null 当前对象的返回值 TimeSpan 用通用格式说明符格式 ( "c" ) 。If format is equal to String.Empty or is null, the return value of the current TimeSpan object is formatted with the common format specifier ("c"). 如果 format 是其他任何值,则该方法将引发 FormatExceptionIf format is any other value, the method throws a FormatException.

如果 format 是标准格式字符串,则返回的字符串的格式由当前区域性的格式设置约定定义。If format is a standard format string, the format of the returned string is defined by the formatting conventions of the current culture.

重要

值的自定义格式字符串 TimeSpan 不包含日期或时间分隔符。The custom format strings for TimeSpan values do not include a date or time separator. 如果要在格式字符串中包含这些元素,则必须将它们视为字符文本。If you want to include these elements in your format string, you must treat them as character literals. 有关详细信息,请参阅示例,并查看 自定义的 TimeSpan 格式字符串 主题。See the example for an illustration, and see the Custom TimeSpan Format Strings topic for more information.

.NET 提供广泛的格式支持,以下格式设置主题对此进行了更详细的介绍:.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

适用于

ToString()

将当前 TimeSpan 对象的值转换为其等效的字符串表示形式。Converts the value of the current TimeSpan object to its equivalent string representation.

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

返回

String

当前 TimeSpan 值的字符串表示形式。The string representation of the current TimeSpan value.

示例

下面的示例显示了通过 ToString 使用多个值调用方法返回的字符串 TimeSpanThe following example displays the strings returned by calling the ToString method with a number of TimeSpan values. 请注意,虽然此示例不直接调用 ToString 方法,但 Console.WriteLine 在尝试将 TimeSpan 值转换为其字符串表示形式时,方法会调用此方法。Note that although the example does not call the ToString method directly, it is called by the Console.WriteLine method when it attempts to convert a TimeSpan value to its string representation.

 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);
 
 // Initalize 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);
 
 // Initalize a time span to 3 hours.
 span = new TimeSpan(3, 0, 0);
 Console.WriteLine(span);
 
 // Initalize 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
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)
      
      ' Initalize 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)
      
      ' Initalize a time span to 3 hours.
      span = New TimeSpan(3, 0, 0)
      Console.WriteLine(span)
      
      ' Initalize 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" 格式说明符并采用以下格式:The returned string is formatted with the "c" format specifier and has the following format:

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

方括号 ( [和] ) 中的元素不能包含在返回的字符串中。Elements in square brackets ([ and ]) may not be included in the returned string. 冒号和句点 (:和。 ) 为原义字符。Colons and periods (: and.) are literal characters. 下表列出了非文本元素。The non-literal elements are listed in the following table. 请注意,方法返回的字符串 ToString() 不区分区域性。Note that the string returned by the ToString() method is not culture-sensitive.

Item 描述Description
"-""-" 负号,指示负时间间隔。A minus sign, which indicates a negative time interval. 对于正时间跨度,不包含任何符号。No sign is included for a positive time span.
"d""d" 时间间隔中的天数。The number of days in the time interval. 如果时间间隔小于一天,则省略此元素。This element is omitted if the time interval is less than one day.
"hh""hh" 时间间隔中的小时数,范围为0到23。The number of hours in the time interval, ranging from 0 to 23.
"mm""mm" 时间间隔中的分钟数,范围为0到59。The number of minutes in the time interval, ranging from 0 to 59.
"ss""ss" 时间间隔中的秒数,范围介于0到59之间。The number of seconds in the time interval, ranging from 0 to 59.
"fffffff""fffffff" 时间间隔中秒的小数部分。Fractional seconds in the time interval. 如果时间间隔不包含秒的小数部分,则省略此元素。This element is omitted if the time interval does not include fractional seconds. 如果存在,则始终使用七个十进制数字表示秒的小数部分。If present, fractional seconds are always expressed using seven decimal digits.

调用方说明

.NET Framework 4 中添加了对格式设置值的支持 TimeSpanSupport for formatting TimeSpan values was added in the .NET Framework 4. 但是, ToString() 方法重载保持不区分区域性。However, the ToString() method overload remains culture-insensitive. 它的行为与以前版本的 .NET Framework 保持不变。Its behavior remains unchanged from previous versions of the .NET Framework. 若要控制值的格式设置 TimeSpan ,请调用 ToString(String)ToString(String, IFormatProvider) 重载。To control the formatting of a TimeSpan value, call the ToString(String) or ToString(String, IFormatProvider) overload.

另请参阅

适用于