Share via


自定义的 TimeSpan 格式字符串

TimeSpan 格式字符串定义由格式设置操作生成的 TimeSpan 值的字符串表示形式。 一个自定义格式字符串包含一个或多个自定义 TimeSpan 格式说明符与任意数量的文本字符。 任何非标准 TimeSpan 格式字符串的字符串被解释为自定义 TimeSpan 格式字符串。

重要说明重要事项

自定义 TimeSpan 格式说明符不包含占位符分隔符,如用于分隔天与小时的符号、用于分隔小时与分钟的符号或用于分隔秒与秒的小数部分的符号。相反,这些符号必须作为字符串文本包含在自定义格式字符串中。例如,"dd\.hh\:mm" 将句点 (.) 定义为日期与小时之间的分隔符,并将冒号 (:) 定义为小时与分钟之间的分隔符。

TimeSpan 值的字符串表示形式由对 TimeSpan.ToString 方法重载的调用和支持复合格式设置(如 String.Format)的方法生成。 有关更多信息,请参见格式化类型复合格式。 下面的示例演示自定义格式字符串在格式设置操作中的用法。

Module Example
   Public Sub Main()
      Dim duration As New TimeSpan(1, 12, 23, 62)

      Dim output As String = Nothing
      output = "Time of Travel: " + duration.ToString("%d") + " days"
      Console.WriteLine(output)
      output = "Time of Travel: " + duration.ToString("dd\.hh\:mm\:ss") 
      Console.WriteLine(output)

      Console.WriteLine("Time of Travel: {0:%d} day(s)", duration)
      Console.WriteLine("Time of Travel: {0:dd\.hh\:mm\:ss} days", duration)
   End Sub
End Module
' The example displays the following output:
'       Time of Travel: 1 days
'       Time of Travel: 01.12:24:02
'       Time of Travel: 1 day(s)
'       Time of Travel: 01.12:24:02 days
using System;

public class Example
{
   public static void Main()
   {
      TimeSpan duration = new TimeSpan(1, 12, 23, 62);

      string output = null;
      output = "Time of Travel: " + duration.ToString("%d") + " days";
      Console.WriteLine(output);
      output = "Time of Travel: " + duration.ToString(@"dd\.hh\:mm\:ss"); 
      Console.WriteLine(output);

      Console.WriteLine("Time of Travel: {0:%d} day(s)", duration);
      Console.WriteLine("Time of Travel: {0:dd\\.hh\\:mm\\:ss} days", duration);
   }
}
// The example displays the following output:
//       Time of Travel: 1 days
//       Time of Travel: 01.12:24:02
//       Time of Travel: 1 day(s)
//       Time of Travel: 01.12:24:02 days

TimeSpan.ParseExactTimeSpan.TryParseExact 方法还可使用自定义 TimeSpan 格式字符串来定义分析操作所需的输入字符串格式。 (分析操作会将值的字符串表示形式转换为该值。)下面的示例演示标准格式字符串在分析操作中的用法。

Module Example
   Public Sub Main()
      Dim value As String = Nothing
      Dim interval As TimeSpan

      value = "6"
      If TimeSpan.TryParseExact(value, "%d", Nothing, interval) Then
         Console.WriteLine("{0} --> {1}", value, interval.ToString("c"))
      Else
         Console.WriteLine("Unable to parse '{0}'", value)
      End If

      value = "16:32.05"
      If TimeSpan.TryParseExact(value, "mm\:ss\.ff", Nothing, interval) Then
         Console.WriteLine("{0} --> {1}", value, interval.ToString("c"))
      Else
         Console.WriteLine("Unable to parse '{0}'", value)
      End If

      value= "12.035"
      If TimeSpan.TryParseExact(value, "ss\.fff", Nothing, interval) Then
         Console.WriteLine("{0} --> {1}", value, interval.ToString("c"))
      Else
         Console.WriteLine("Unable to parse '{0}'", value)
      End If
   End Sub
End Module
' The example displays the following output:
'       6 --> 6.00:00:00
'       16:32.05 --> 00:16:32.0500000
'       12.035 --> 00:00:12.0350000
using System;

public class Example
{
   public static void Main()
   {
      string value = null;
      TimeSpan interval;

      value = "6";
      if (TimeSpan.TryParseExact(value, "%d", null, out interval))
         Console.WriteLine("{0} --> {1}", value, interval.ToString("c"));
      else
         Console.WriteLine("Unable to parse '{0}'", value);

      value = "16:32.05";
      if (TimeSpan.TryParseExact(value, @"mm\:ss\.ff", null, out interval))
         Console.WriteLine("{0} --> {1}", value, interval.ToString("c"));
      else
         Console.WriteLine("Unable to parse '{0}'", value);

      value= "12.035";
      if (TimeSpan.TryParseExact(value, "ss\\.fff", null, out interval))
         Console.WriteLine("{0} --> {1}", value, interval.ToString("c"));
      else
         Console.WriteLine("Unable to parse '{0}'", value);
   }
}
// The example displays the following output:
//       6 --> 6.00:00:00
//       16:32.05 --> 00:16:32.0500000
//       12.035 --> 00:00:12.0350000

下表描述了自定义日期和时间格式说明符。

格式说明符

说明

示例

“d”、“%d”

时间间隔中的整天数。

有关更多信息,请参见“d”自定义格式说明符。

new TimeSpan(6, 14, 32, 17, 685):

   %d --> "6"

   d\.hh\:mm --> "6.14:32"

“dd”-“dddddddd”

时间间隔中的整天数,可以根据需要用前导零填充该数字。

有关更多信息,请参见“dd”-“dddddddd”自定义格式说明符。

new TimeSpan(6, 14, 32, 17, 685):

   ddd --> "006"

   dd\.hh\:mm --> "06.14:32"

“h”、“%h”

时间间隔中未计入天部分中的整小时数。 一位数的小时数没有前导零。

有关更多信息,请参见“h”自定义格式说明符。

new TimeSpan(6, 14, 32, 17, 685):

   %h --> "14"

   hh\:mm --> "14:32"

“hh”

时间间隔中未计入天部分中的整小时数。 一位数的小时数有一个前导零。

有关更多信息,请参见“hh”自定义格式说明符。

new TimeSpan(6, 14, 32, 17, 685):

   hh --> "14"

new TimeSpan(6, 8, 32, 17, 685):

   hh --> 08

“m”、“%m”

时间间隔中未计入小时或天部分的整分钟数。 一位数的分钟数没有前导零。

有关更多信息,请参见“m”自定义格式说明符。

new TimeSpan(6, 14, 8, 17, 685):

   %m --> "8"

   h\:m --> "14:8"

“mm”

时间间隔中未计入小时或天部分的整分钟数。 一位数的分钟数有一个前导零。

有关更多信息,请参见“mm”自定义格式说明符。

new TimeSpan(6, 14, 8, 17, 685):

   mm --> "08"

new TimeSpan(6, 8, 5, 17, 685):

   d\.hh\:mm\:ss --> 6.08:05:17

“s”、“%s”

时间间隔中未计入小时、天或分钟部分的整秒数。 一位数的秒数没有前导零。

有关更多信息,请参见“s”自定义格式说明符。

TimeSpan.FromSeconds(12.965):

   %s --> 12

   s\.fff --> 12.965

“ss”

时间间隔中未计入小时、天或分钟部分的整秒数。 一位数的秒数有一个前导零。

有关更多信息,请参见“ss”自定义格式说明符。

TimeSpan.FromSeconds(6.965):

   ss --> 06

   ss\.fff --> 06.965

“f”、“%f”

时间间隔中的十分之几秒。

有关更多信息,请参见“f”自定义格式说明符。

TimeSpan.FromSeconds(6.895):

   f --> 8

   ss\.f --> 06.8

“ff”

时间间隔中的百分之几秒。

有关更多信息,请参见“ff”自定义格式说明符。

TimeSpan.FromSeconds(6.895):

   ff --> 89

   ss\.ff --> 06.89

“fff”

时间间隔中的毫秒。

有关更多信息,请参见“fff”自定义格式说明符。

TimeSpan.FromSeconds(6.895):

   fff --> 895

   ss\.fff --> 06.895

“ffff”

时间间隔中的万分之几秒。

有关更多信息,请参见“ffff”自定义格式说明符。

TimeSpan.Parse("0:0:6.8954321"):

   ffff --> 8954

   ss\.ffff --> 06.8954

“fffff”

时间间隔中的十万分之几秒。

有关更多信息,请参见“fffff”自定义格式说明符。

TimeSpan.Parse("0:0:6.8954321"):

   fffff --> 89543

   ss\.fffff --> 06.89543

“ffffff”

时间间隔中的百万分之几秒。

有关更多信息,请参见“ffffff”自定义格式说明符。

TimeSpan.Parse("0:0:6.8954321"):

   ffffff --> 895432

   ss\.ffffff --> 06.895432

“fffffff”

时间间隔中的千万分之几秒(或小数嘀嗒数)。

有关更多信息,请参见“fffffff”自定义格式说明符。

TimeSpan.Parse("0:0:6.8954321"):

   fffffff --> 8954321

   ss\.fffffff --> 06.8954321

“F”、“%F”

时间间隔中的十分之几秒。 如果该数字为零,则不显示任何内容。

有关更多信息,请参见“F”自定义格式说明符。

TimeSpan.Parse("00:00:06.32"):

   %F: 3

TimeSpan.Parse("0:0:3.091"):

   ss\.F: 03.

“FF”

时间间隔中的百分之几秒。 不包含任何小数尾随零或两个零位。

有关更多信息,请参见“FF”自定义格式说明符。

TimeSpan.Parse("00:00:06.329"):

   FF: 32

TimeSpan.Parse("0:0:3.101"):

   ss\.FF: 03.1

“FFF”

时间间隔中的毫秒。 不包含任何小数尾随零。

更多信息:

TimeSpan.Parse("00:00:06.3291"):

   FFF: 329

TimeSpan.Parse("0:0:3.1009"):

   ss\.FFF: 03.1

“FFFF”

时间间隔中的万分之几秒。 不包含任何小数尾随零。

有关更多信息,请参见“FFFF”自定义格式说明符。

TimeSpan.Parse("00:00:06.32917"):

   FFFFF: 3291

TimeSpan.Parse("0:0:3.10009"):

   ss\.FFFF: 03.1

“FFFFF”

时间间隔中的十万分之几秒。 不包含任何小数尾随零。

有关更多信息,请参见“FFFFF”自定义格式说明符。

TimeSpan.Parse("00:00:06.329179"):

   FFFFF: 32917

TimeSpan.Parse("0:0:3.100009"):

   ss\.FFFFF: 03.1

“FFFFFF”

时间间隔中的百万分之几秒。 不显示任何小数尾随零。

有关更多信息,请参见“FFFFFF”自定义格式说明符。

TimeSpan.Parse("00:00:06.3291791"):

   FFFFFF: 329179

TimeSpan.Parse("0:0:3.1000009"):

   ss\.FFFFFF: 03.1

“FFFFFFF”

时间间隔中的千万分之几秒。 不显示任何小数尾随零或七个零。

有关更多信息,请参见“FFFFFFF”自定义格式说明符。

TimeSpan.Parse("00:00:06.3291791"):

   FFFFFF: 3291791

TimeSpan.Parse("0:0:3.1900000"):

   ss\.FFFFFF: 03.19

“string”

文本字符串分隔符。

有关更多信息,请参见其他字符。

new TimeSpan(14, 32, 17):

   hh':'mm':'ss --> "14:32:17"

\

转义字符。

有关更多信息,请参见其他字符。

new TimeSpan(14, 32, 17):

   hh\:mm\:ss --> "14:32:17"

任何其他字符

任意其他非转义字符被解释为自定义格式说明符。

有关更多信息,请参见其他字符。

new TimeSpan(14, 32, 17):

   hh\:mm\:ss --> "14:32:17"

“d”自定义格式说明符

“d”自定义格式说明符输出 TimeSpan.Days 属性的值,该值表示时间间隔中的整天数。 它会输出 TimeSpan 值中的整天数,即使该值的位数大于一。 如果 TimeSpan.Days 属性的值为零,则此说明符输出“0”。

如果单独使用“d”自定义格式说明符,请指定“%d”,以确保它不被错误解释为标准格式字符串。 下面的示例进行了这方面的演示。

Dim ts As New TimeSpan(16, 4, 3, 17, 250)
Console.WriteLine(ts.ToString("%d"))
' Displays 16   
TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
Console.WriteLine(ts1.ToString("%d"));
// Displays 16   

下面的示例阐释“d”自定义格式说明符的用法。

Dim ts2 As New TimeSpan(4, 3, 17)
Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

Dim ts3 As New TimeSpan(3, 4, 3, 17)
Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
' The example displays the following output:
'       0.04:03:17
'       3.04:03:17      
TimeSpan ts2 = new TimeSpan(4, 3, 17);
Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
// The example displays the following output:
//       0.04:03:17
//       3.04:03:17      

返回表首

“dd”-“dddddddd”自定义格式说明符

“dd”、“ddd”、“dddd”、“ddddd”、“dddddd”、“ddddddd”和“dddddddd”自定义格式说明符输出 TimeSpan.Days 属性的值,该值表示时间间隔中的整天数。

输出字符串包含一个由格式说明符中的“d”字符数指定的最小位数的数字,可以根据需要用前导零填充此数字。 如果天数中的位数超过格式说明符中的“d”字符的数目,则在结果字符串中输出整天数。

下面的示例使用这些格式说明符显示两个 TimeSpan 值的字符串表示形式。 第一个时间间隔的天部分的值为零;第二个时间间隔的天部分的值为 365。

Dim ts1 As New TimeSpan(0, 23, 17, 47)
Dim ts2 As New TimeSpan(365, 21, 19, 45)

For ctr As Integer = 2 To 8
   Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
   Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1) 
   Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
   Console.WriteLine()
Next  
' The example displays the following output:
'       dd\.hh\:mm\:ss --> 00.23:17:47
'       dd\.hh\:mm\:ss --> 365.21:19:45
'       
'       ddd\.hh\:mm\:ss --> 000.23:17:47
'       ddd\.hh\:mm\:ss --> 365.21:19:45
'       
'       dddd\.hh\:mm\:ss --> 0000.23:17:47
'       dddd\.hh\:mm\:ss --> 0365.21:19:45
'       
'       ddddd\.hh\:mm\:ss --> 00000.23:17:47
'       ddddd\.hh\:mm\:ss --> 00365.21:19:45
'       
'       dddddd\.hh\:mm\:ss --> 000000.23:17:47
'       dddddd\.hh\:mm\:ss --> 000365.21:19:45
'       
'       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
'       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
'       
'       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
'       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

for (int ctr = 2; ctr <= 8; ctr++)
{
   string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
   Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1);  
   Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2);
   Console.WriteLine();
}  
// The example displays the following output:
//       dd\.hh\:mm\:ss --> 00.23:17:47
//       dd\.hh\:mm\:ss --> 365.21:19:45
//       
//       ddd\.hh\:mm\:ss --> 000.23:17:47
//       ddd\.hh\:mm\:ss --> 365.21:19:45
//       
//       dddd\.hh\:mm\:ss --> 0000.23:17:47
//       dddd\.hh\:mm\:ss --> 0365.21:19:45
//       
//       ddddd\.hh\:mm\:ss --> 00000.23:17:47
//       ddddd\.hh\:mm\:ss --> 00365.21:19:45
//       
//       dddddd\.hh\:mm\:ss --> 000000.23:17:47
//       dddddd\.hh\:mm\:ss --> 000365.21:19:45
//       
//       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
//       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
//       
//       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
//       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      

返回表首

“h”自定义格式说明符

“h”自定义格式说明符输出 TimeSpan.Hours 属性的值,该值表示时间间隔中的未计入天部分中的整小时数。 如果 TimeSpan.Hours 属性的值为 0 到 9,则此说明符返回一位数的字符串值;如果 TimeSpan.Hours 属性的值为 10 到 23,则此说明符返回两位数的字符串值。

如果单独使用“h”自定义格式说明符,请指定“%h”,以确保它不被错误解释为标准格式字符串。 下面的示例进行了这方面的演示。

Dim ts As New TimeSpan(3, 42, 0)
Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
' The example displays the following output:
'       3 hours 42 minutes
TimeSpan ts = new TimeSpan(3, 42, 0);
Console.WriteLine("{0:%h} hours {0:%m} minutes", ts);
// The example displays the following output:
//       3 hours 42 minutes

通常,在分析操作中,只包含一个数字的输入字符串被解释为天数。 可以改用“%h”自定义格式说明符以将数值字符串解释为小时数。 下面的示例进行了这方面的演示。

Dim value As String = "8"
Dim interval As TimeSpan
If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
   Console.WriteLine(interval.ToString("c"))
Else
   Console.WriteLine("Unable to convert '{0}' to a time interval", 
                     value)   
End If   
' The example displays the following output:
'       08:00:00                              
string value = "8";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "%h", null, out interval))
   Console.WriteLine(interval.ToString("c"));
else
   Console.WriteLine("Unable to convert '{0}' to a time interval", 
                     value);   
// The example displays the following output:
//       08:00:00                              

下面的示例阐释“h”自定义格式说明符的用法。

Dim ts1 As New TimeSpan(14, 3, 17)
Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

Dim ts2 As New TimeSpan(3, 4, 3, 17)
Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
' The example displays the following output:
'       0.14:03:17
'       3.4:03:17
TimeSpan ts1 = new TimeSpan(14, 3, 17);
Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
// The example displays the following output:
//       0.14:03:17
//       3.4:03:17

返回表首

“hh”自定义格式说明符

“hh”自定义格式说明符输出 TimeSpan.Hours 属性的值,该值表示时间间隔中的未计入天部分中的整小时数。 对于从 0 到 9 的值,输出字符串包含一个前导零。

通常,在分析操作中,只包含一个数字的输入字符串被解释为天数。 可以改用“hh”自定义格式说明符以将数值字符串解释为小时数。 下面的示例进行了这方面的演示。

Dim value As String = "08"
Dim interval As TimeSpan
If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
   Console.WriteLine(interval.ToString("c"))
Else
   Console.WriteLine("Unable to convert '{0}' to a time interval", 
                     value)   
End If   
' The example displays the following output:
'       08:00:00                              
string value = "08";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "hh", null, out interval))
   Console.WriteLine(interval.ToString("c"));
else
   Console.WriteLine("Unable to convert '{0}' to a time interval", 
                     value);   
// The example displays the following output:
//       08:00:00                              

下面的示例阐释“hh”自定义格式说明符的用法。

Dim ts1 As New TimeSpan(14, 3, 17)
Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

Dim ts2 As New TimeSpan(3, 4, 3, 17)
Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
' The example displays the following output:
'       0.14:03:17
'       3.04:03:17
TimeSpan ts1 = new TimeSpan(14, 3, 17);
Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
// The example displays the following output:
//       0.14:03:17
//       3.04:03:17

返回表首

“m”自定义格式说明符

“m”自定义格式说明符输出 TimeSpan.Minutes 属性的值,该值表示时间间隔中的未计入天部分中的整分钟数。 如果 TimeSpan.Minutes 属性的值为 0 到 9,则此说明符返回一位数的字符串值;如果 TimeSpan.Minutes 属性的值的为 10 到 59,则此说明符返回两位数的字符串值。

如果单独使用“m”自定义格式说明符,请指定“%m”,以确保它不被错误解释为标准格式字符串。 下面的示例进行了这方面的演示。

Dim ts As New TimeSpan(3, 42, 0)
Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
' The example displays the following output:
'       3 hours 42 minutes
TimeSpan ts = new TimeSpan(3, 42, 0);
Console.WriteLine("{0:%h} hours {0:%m} minutes", ts);
// The example displays the following output:
//       3 hours 42 minutes

通常,在分析操作中,只包含一个数字的输入字符串被解释为天数。 可以改用“%m”自定义格式说明符以将数值字符串解释为分钟数。 下面的示例进行了这方面的演示。

Dim value As String = "3"
Dim interval As TimeSpan
If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
   Console.WriteLine(interval.ToString("c"))
Else
   Console.WriteLine("Unable to convert '{0}' to a time interval", 
                     value)   
End If   
' The example displays the following output:
'       00:03:00                              
string value = "3";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "%m", null, out interval))
   Console.WriteLine(interval.ToString("c"));
else
   Console.WriteLine("Unable to convert '{0}' to a time interval", 
                     value);   
// The example displays the following output:
//       00:03:00                              

下面的示例阐释“m”自定义格式说明符的用法。

Dim ts1 As New TimeSpan(0, 6, 32)
Console.WriteLine("{0:m\:ss} minutes", ts1)

Dim ts2 As New TimeSpan(0, 18, 44)
Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
' The example displays the following output:
'       6:32 minutes
'       Elapsed time: 18:44
TimeSpan ts1 = new TimeSpan(0, 6, 32);
Console.WriteLine("{0:m\\:ss} minutes", ts1);

TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
Console.WriteLine("Elapsed time: {0:m\\:ss}", ts2);
// The example displays the following output:
//       6:32 minutes
//       Elapsed time: 18:44

返回表首

“mm”自定义格式说明符

“mm”自定义格式说明符输出 TimeSpan.Minutes 属性的值,该值表示时间间隔中的未计入小时或天部分中的整分钟数。 对于从 0 到 9 的值,输出字符串包含一个前导零。

通常,在分析操作中,只包含一个数字的输入字符串被解释为天数。 可以改用“mm”自定义格式说明符以将数值字符串解释为分钟数。 下面的示例进行了这方面的演示。

Dim value As String = "05"
Dim interval As TimeSpan
If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
   Console.WriteLine(interval.ToString("c"))
Else
   Console.WriteLine("Unable to convert '{0}' to a time interval", 
                     value)   
End If   
' The example displays the following output:
'       00:05:00           
string value = "07";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "mm", null, out interval))
   Console.WriteLine(interval.ToString("c"));
else
   Console.WriteLine("Unable to convert '{0}' to a time interval", 
                     value);   
// The example displays the following output:
//       00:07:00                              

下面的示例阐释“mm”自定义格式说明符的用法。

Dim departTime As New TimeSpan(11, 12, 00)
Dim arriveTime As New TimeSpan(16, 28, 00)
Console.WriteLine("Travel time: {0:hh\:mm}", 
                  arriveTime - departTime)
' The example displays the following output:
'       Travel time: 05:16      
TimeSpan departTime = new TimeSpan(11, 12, 00);
TimeSpan arriveTime = new TimeSpan(16, 28, 00);
Console.WriteLine("Travel time: {0:hh\\:mm}", 
                  arriveTime - departTime);
// The example displays the following output:
//       Travel time: 05:16      

返回表首

“s”自定义格式说明符

“s”自定义格式说明符输出 TimeSpan.Seconds 属性的值,该值表示时间间隔中的未计入小时、天或分钟部分的整秒数。 如果 TimeSpan.Seconds 属性的值为 0 到 9,则此字符串返回一位数的字符串值;如果 TimeSpan.Seconds 属性的值为 10 到 59,则此字符串返回两位数的字符串值。

如果单独使用“s”自定义格式说明符,请指定“%s”,以确保它不被错误解释为标准格式字符串。 下面的示例进行了这方面的演示。

Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
Console.WriteLine(ts.ToString("%s"))
' The example displays the following output:
'       12
TimeSpan ts = TimeSpan.FromSeconds(12.465);
Console.WriteLine(ts.ToString("%s"));
// The example displays the following output:
//       12

通常,在分析操作中,只包含一个数字的输入字符串被解释为天数。 可以改用“%s”自定义格式说明符以将数值字符串解释为秒数。 下面的示例进行了这方面的演示。

Dim value As String = "9"
Dim interval As TimeSpan
If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
   Console.WriteLine(interval.ToString("c"))
Else
   Console.WriteLine("Unable to convert '{0}' to a time interval", 
                     value)   
End If   
' The example displays the following output:
'       00:00:09
string value = "9";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "%s", null, out interval))
   Console.WriteLine(interval.ToString("c"));
else
   Console.WriteLine("Unable to convert '{0}' to a time interval", 
                     value);   
// The example displays the following output:
//       00:00:09

下面的示例阐释“s”自定义格式说明符的用法。

Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
Console.WriteLine("Elapsed Time: {0:s\:fff} seconds", 
                  endTime - startTime)
' The example displays the following output:
'       Elapsed Time: 6:003 seconds      
TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds", 
                  endTime - startTime);
// The example displays the following output:
//       Elapsed Time: 6:003 seconds      

返回表首

“ss”自定义格式说明符

“ss”自定义格式说明符输出 TimeSpan.Seconds 属性的值,该值表示时间间隔中的未计入小时、天或分钟部分的整秒数。 对于从 0 到 9 的值,输出字符串包含一个前导零。

通常,在分析操作中,只包含一个数字的输入字符串被解释为天数。 可以改用“ss”自定义格式说明符以将数值字符串解释为秒数。 下面的示例进行了这方面的演示。

Dim values() As String = { "49", "9", "06" }
Dim interval As TimeSpan
For Each value As String In values
   If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
      Console.WriteLine(interval.ToString("c"))
   Else
      Console.WriteLine("Unable to convert '{0}' to a time interval", 
                        value)   
   End If   
Next   
' The example displays the following output:
'       00:00:49
'       Unable to convert '9' to a time interval
'       00:00:06
string[] values = { "49", "9", "06" };
TimeSpan interval;
foreach (string value in values)
{
   if (TimeSpan.TryParseExact(value, "ss", null, out interval))
      Console.WriteLine(interval.ToString("c"));
   else
      Console.WriteLine("Unable to convert '{0}' to a time interval", 
                        value);   
}
// The example displays the following output:
//       00:00:49
//       Unable to convert '9' to a time interval
//       00:00:06

下面的示例阐释“ss”自定义格式说明符的用法。

Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
Console.WriteLine(interval1.ToString("ss\.fff"))
Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
Console.WriteLine(interval2.ToString("ss\.fff"))
' The example displays the following output:
'       12.600
'       06.485
TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
Console.WriteLine(interval1.ToString(@"ss\.fff"));

TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
Console.WriteLine(interval2.ToString(@"ss\.fff"));
// The example displays the following output:
//       12.600
//       06.485

返回表首

“f”自定义格式说明符

“f”自定义格式说明符输出时间间隔中的十分之几秒。 在格式设置操作中,任何其余的小数位将被截断。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,输入字符串必须只包含一个小数位。

如果单独使用“f”自定义格式说明符,请指定“%f”,以确保它不被错误解释为标准格式字符串。

下面的示例使用“f”自定义格式说明符显示 TimeSpan 值中的十分之几秒。“ f”首先只会用作格式说明符,然后再与自定义格式字符串中的“s”说明符一起使用。

Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   If fmt.Length = 1 Then fmt = "%" + fmt
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next 
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' The example displays the following output:
'            %f: 8
'            ff: 87
'           fff: 876
'          ffff: 8765
'         fffff: 87654
'        ffffff: 876543
'       fffffff: 8765432
'    
'           s\.f: 29.8
'          s\.ff: 29.87
'         s\.fff: 29.876
'        s\.ffff: 29.8765
'       s\.fffff: 29.87654
'      s\.ffffff: 29.876543
'     s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432);
string fmt;
Console.WriteLine(ts.ToString("c"));
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   if (fmt.Length == 1) fmt = "%" + fmt;
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts);
} 
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts);
}
// The example displays the following output:
//               %f: 8
//               ff: 87
//              fff: 876
//             ffff: 8765
//            fffff: 87654
//           ffffff: 876543
//          fffffff: 8765432
//       
//              s\.f: 29.8
//             s\.ff: 29.87
//            s\.fff: 29.876
//           s\.ffff: 29.8765
//          s\.fffff: 29.87654
//         s\.ffffff: 29.876543
//        s\.fffffff: 29.8765432      

返回表首

“ff”自定义格式说明符

“ff”自定义格式说明符输出时间间隔中的百分之几秒。 在格式设置操作中,任何其余的小数位将被截断。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,输入字符串必须只包含两个小数位。

下面的示例使用“ff”自定义格式说明符显示 TimeSpan 值中的百分之几秒。“ ff”首先只会用作格式说明符,然后再与自定义格式字符串中的“s”说明符一起使用。

Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   If fmt.Length = 1 Then fmt = "%" + fmt
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next 
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' The example displays the following output:
'            %f: 8
'            ff: 87
'           fff: 876
'          ffff: 8765
'         fffff: 87654
'        ffffff: 876543
'       fffffff: 8765432
'    
'           s\.f: 29.8
'          s\.ff: 29.87
'         s\.fff: 29.876
'        s\.ffff: 29.8765
'       s\.fffff: 29.87654
'      s\.ffffff: 29.876543
'     s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432);
string fmt;
Console.WriteLine(ts.ToString("c"));
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   if (fmt.Length == 1) fmt = "%" + fmt;
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts);
} 
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts);
}
// The example displays the following output:
//               %f: 8
//               ff: 87
//              fff: 876
//             ffff: 8765
//            fffff: 87654
//           ffffff: 876543
//          fffffff: 8765432
//       
//              s\.f: 29.8
//             s\.ff: 29.87
//            s\.fff: 29.876
//           s\.ffff: 29.8765
//          s\.fffff: 29.87654
//         s\.ffffff: 29.876543
//        s\.fffffff: 29.8765432      

返回表首

“fff”自定义格式说明符

“fff”自定义格式说明符(带有三个“f”字符)输出时间间隔中的毫秒。 在格式设置操作中,任何其余的小数位将被截断。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,输入字符串必须只包含三个小数位。

下面的示例使用“fff”自定义格式说明符显示 TimeSpan 值中的毫秒。“ fff”首先只会用作格式说明符,然后再与自定义格式字符串中的“s”说明符一起使用。

Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   If fmt.Length = 1 Then fmt = "%" + fmt
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next 
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' The example displays the following output:
'            %f: 8
'            ff: 87
'           fff: 876
'          ffff: 8765
'         fffff: 87654
'        ffffff: 876543
'       fffffff: 8765432
'    
'           s\.f: 29.8
'          s\.ff: 29.87
'         s\.fff: 29.876
'        s\.ffff: 29.8765
'       s\.fffff: 29.87654
'      s\.ffffff: 29.876543
'     s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432);
string fmt;
Console.WriteLine(ts.ToString("c"));
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   if (fmt.Length == 1) fmt = "%" + fmt;
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts);
} 
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts);
}
// The example displays the following output:
//               %f: 8
//               ff: 87
//              fff: 876
//             ffff: 8765
//            fffff: 87654
//           ffffff: 876543
//          fffffff: 8765432
//       
//              s\.f: 29.8
//             s\.ff: 29.87
//            s\.fff: 29.876
//           s\.ffff: 29.8765
//          s\.fffff: 29.87654
//         s\.ffffff: 29.876543
//        s\.fffffff: 29.8765432      

返回表首

“ffff”自定义格式说明符

“ffff”自定义格式说明符(带有四个“f”字符)输出时间间隔中的万分之几秒。 在格式设置操作中,任何其余的小数位将被截断。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,输入字符串必须只包含四个小数位。

下面的示例使用“ffff”自定义格式说明符显示 TimeSpan 值中的万分之几秒。“ ffff”首先只会用作格式说明符,然后再与自定义格式字符串中的“s”说明符一起使用。

Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   If fmt.Length = 1 Then fmt = "%" + fmt
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next 
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' The example displays the following output:
'            %f: 8
'            ff: 87
'           fff: 876
'          ffff: 8765
'         fffff: 87654
'        ffffff: 876543
'       fffffff: 8765432
'    
'           s\.f: 29.8
'          s\.ff: 29.87
'         s\.fff: 29.876
'        s\.ffff: 29.8765
'       s\.fffff: 29.87654
'      s\.ffffff: 29.876543
'     s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432);
string fmt;
Console.WriteLine(ts.ToString("c"));
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   if (fmt.Length == 1) fmt = "%" + fmt;
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts);
} 
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts);
}
// The example displays the following output:
//               %f: 8
//               ff: 87
//              fff: 876
//             ffff: 8765
//            fffff: 87654
//           ffffff: 876543
//          fffffff: 8765432
//       
//              s\.f: 29.8
//             s\.ff: 29.87
//            s\.fff: 29.876
//           s\.ffff: 29.8765
//          s\.fffff: 29.87654
//         s\.ffffff: 29.876543
//        s\.fffffff: 29.8765432      

返回表首

“fffff”自定义格式说明符

“fffff”自定义格式说明符(带有五个“f”字符)输出时间间隔中的十万分之几秒。 在格式设置操作中,任何其余的小数位将被截断。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,输入字符串必须只包含五个小数位。

下面的示例使用“fffff”自定义格式说明符显示 TimeSpan 值中的十万分之几秒。“ fffff”首先只会用作格式说明符,然后再与自定义格式字符串中的“s”说明符一起使用。

Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   If fmt.Length = 1 Then fmt = "%" + fmt
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next 
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' The example displays the following output:
'            %f: 8
'            ff: 87
'           fff: 876
'          ffff: 8765
'         fffff: 87654
'        ffffff: 876543
'       fffffff: 8765432
'    
'           s\.f: 29.8
'          s\.ff: 29.87
'         s\.fff: 29.876
'        s\.ffff: 29.8765
'       s\.fffff: 29.87654
'      s\.ffffff: 29.876543
'     s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432);
string fmt;
Console.WriteLine(ts.ToString("c"));
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   if (fmt.Length == 1) fmt = "%" + fmt;
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts);
} 
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts);
}
// The example displays the following output:
//               %f: 8
//               ff: 87
//              fff: 876
//             ffff: 8765
//            fffff: 87654
//           ffffff: 876543
//          fffffff: 8765432
//       
//              s\.f: 29.8
//             s\.ff: 29.87
//            s\.fff: 29.876
//           s\.ffff: 29.8765
//          s\.fffff: 29.87654
//         s\.ffffff: 29.876543
//        s\.fffffff: 29.8765432      

返回表首

“ffffff”自定义格式说明符

“ffffff”自定义格式说明符(带有六个“f”字符)输出时间间隔中的百万分之几秒。 在格式设置操作中,任何其余的小数位将被截断。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,输入字符串必须只包含六个小数位。

下面的示例使用“ffffff”自定义格式说明符显示 TimeSpan 值中的百万分之几秒。 它首先只会用作格式说明符,然后再与自定义格式字符串中的“s”说明符一起使用。

Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   If fmt.Length = 1 Then fmt = "%" + fmt
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next 
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' The example displays the following output:
'            %f: 8
'            ff: 87
'           fff: 876
'          ffff: 8765
'         fffff: 87654
'        ffffff: 876543
'       fffffff: 8765432
'    
'           s\.f: 29.8
'          s\.ff: 29.87
'         s\.fff: 29.876
'        s\.ffff: 29.8765
'       s\.fffff: 29.87654
'      s\.ffffff: 29.876543
'     s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432);
string fmt;
Console.WriteLine(ts.ToString("c"));
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   if (fmt.Length == 1) fmt = "%" + fmt;
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts);
} 
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts);
}
// The example displays the following output:
//               %f: 8
//               ff: 87
//              fff: 876
//             ffff: 8765
//            fffff: 87654
//           ffffff: 876543
//          fffffff: 8765432
//       
//              s\.f: 29.8
//             s\.ff: 29.87
//            s\.fff: 29.876
//           s\.ffff: 29.8765
//          s\.fffff: 29.87654
//         s\.ffffff: 29.876543
//        s\.fffffff: 29.8765432      

返回表首

“fffffff”自定义格式说明符

“fffffff”自定义格式说明符(带有七个“f”字符)输出时间间隔中的千万分之几秒(或小数嘀嗒数)。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,输入字符串必须只包含七个小数位。

下面的示例使用“fffffff”自定义格式说明符显示 TimeSpan 值中的小数嘀嗒数。 它首先只会用作格式说明符,然后再与自定义格式字符串中的“s”说明符一起使用。

Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   If fmt.Length = 1 Then fmt = "%" + fmt
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next 
Console.WriteLine()

For ctr = 1 To 7
   fmt = New String("f"c, ctr)
   Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' The example displays the following output:
'            %f: 8
'            ff: 87
'           fff: 876
'          ffff: 8765
'         fffff: 87654
'        ffffff: 876543
'       fffffff: 8765432
'    
'           s\.f: 29.8
'          s\.ff: 29.87
'         s\.fff: 29.876
'        s\.ffff: 29.8765
'       s\.fffff: 29.87654
'      s\.ffffff: 29.876543
'     s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432);
string fmt;
Console.WriteLine(ts.ToString("c"));
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   if (fmt.Length == 1) fmt = "%" + fmt;
   Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts);
} 
Console.WriteLine();

for (int ctr = 1; ctr <= 7; ctr++) {
   fmt = new String('f', ctr);
   Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts);
}
// The example displays the following output:
//               %f: 8
//               ff: 87
//              fff: 876
//             ffff: 8765
//            fffff: 87654
//           ffffff: 876543
//          fffffff: 8765432
//       
//              s\.f: 29.8
//             s\.ff: 29.87
//            s\.fff: 29.876
//           s\.ffff: 29.8765
//          s\.fffff: 29.87654
//         s\.ffffff: 29.876543
//        s\.fffffff: 29.8765432      

返回表首

“F”自定义格式说明符

“F”自定义格式说明符输出时间间隔中的十分之几秒。 在格式设置操作中,任何其余的小数位将被截断。 如果时间间隔的十分之几秒的值为零,则它不会包含在结果字符串中。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,可以选择显示十分之几秒数。

如果单独使用“F”自定义格式说明符,请指定“%F”,以确保它不被错误解释为标准格式字符串。

下面的示例使用“F”自定义格式说明符显示 TimeSpan 值中的十分之几秒。 此外,该示例还在分析操作中使用此自定义格式说明符。

Console.WriteLine("Formatting:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.669")
Console.WriteLine("{0} ('%F') --> {0:%F}", ts1)

Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.091")
Console.WriteLine("{0} ('ss\.F') --> {0:ss\.F}", ts2)
Console.WriteLine()

Console.WriteLine("Parsing:")
Dim inputs() As String = { "0:0:03.", "0:0:03.1", "0:0:03.12" }
Dim fmt As String = "h\:m\:ss\.F"
Dim ts3 As TimeSpan

For Each input As String In inputs
   If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
   Else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt)
   End If  
Next
' The example displays the following output:
'       Formatting:
'       00:00:03.6690000 ('%F') --> 6
'       00:00:03.0910000 ('ss\.F') --> 03.
'       
'       Parsing:
'       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
'       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
'       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.      
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.669");
Console.WriteLine("{0} ('%F') --> {0:%F}", ts1);

TimeSpan ts2 = TimeSpan.Parse("0:0:3.091");
Console.WriteLine("{0} ('ss\\.F') --> {0:ss\\.F}", ts2);
Console.WriteLine();

Console.WriteLine("Parsing:");
string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.12" };
string fmt = @"h\:m\:ss\.F";
TimeSpan ts3;

foreach (string input in inputs) {
   if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3);
   else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt);
}                        
// The example displays the following output:
//       Formatting:
//       00:00:03.6690000 ('%F') --> 6
//       00:00:03.0910000 ('ss\.F') --> 03.
//       
//       Parsing:
//       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
//       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
//       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.      

返回表首

“FF”自定义格式说明符

“FF”自定义格式说明符输出时间间隔中的百分之几秒。 在格式设置操作中,任何其余的小数位将被截断。 如果小数部分有任何尾随零,则它们不会包含在结果字符串中。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,可以选择显示十分之几秒数和百分之几秒数。

下面的示例使用“FF”自定义格式说明符显示 TimeSpan 值中的百分之几秒。 此外,该示例还在分析操作中使用此自定义格式说明符。

Console.WriteLine("Formatting:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697")
Console.WriteLine("{0} ('FF') --> {0:FF}", ts1)

Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.809")
Console.WriteLine("{0} ('ss\.FF') --> {0:ss\.FF}", ts2)
Console.WriteLine()

Console.WriteLine("Parsing:")
Dim inputs() As String = { "0:0:03.", "0:0:03.1", "0:0:03.127" }
Dim fmt As String = "h\:m\:ss\.FF"
Dim ts3 As TimeSpan

For Each input As String In inputs
   If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
   Else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt)
   End If  
Next
' The example displays the following output:
'       Formatting:
'       00:00:03.6970000 ('FF') --> 69
'       00:00:03.8090000 ('ss\.FF') --> 03.8
'       
'       Parsing:
'       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
'       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
'       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.697");
Console.WriteLine("{0} ('FF') --> {0:FF}", ts1);

TimeSpan ts2 = TimeSpan.Parse("0:0:3.809");
Console.WriteLine("{0} ('ss\\.FF') --> {0:ss\\.FF}", ts2);
Console.WriteLine();

Console.WriteLine("Parsing:");
string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.127" };
string fmt = @"h\:m\:ss\.FF";
TimeSpan ts3;

foreach (string input in inputs) {
   if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3);
   else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt);
}
// The example displays the following output:
//       Formatting:
//       00:00:03.6970000 ('FF') --> 69
//       00:00:03.8090000 ('ss\.FF') --> 03.8
//       
//       Parsing:
//       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
//       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
//       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.      

返回表首

“FFF”自定义格式说明符

“FFF”自定义格式说明符(带有三个“F”字符)输出时间间隔中的毫秒。 在格式设置操作中,任何其余的小数位将被截断。 如果小数部分有任何尾随零,则它们不会包含在结果字符串中。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,可以选择显示十分之几秒数、百分之几秒数和千分之几秒数。

下面的示例使用“FFF”自定义格式说明符显示 TimeSpan 值中的千分之几秒。 此外,该示例还在分析操作中使用此自定义格式说明符。

Console.WriteLine("Formatting:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974")
Console.WriteLine("{0} ('FFF') --> {0:FFF}", ts1)

Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8009")
Console.WriteLine("{0} ('ss\.FFF') --> {0:ss\.FFF}", ts2)
Console.WriteLine()

Console.WriteLine("Parsing:")
Dim inputs() As String = { "0:0:03.", "0:0:03.12", "0:0:03.1279" }
Dim fmt As String = "h\:m\:ss\.FFF"
Dim ts3 As TimeSpan

For Each input As String In inputs
   If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
   Else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt)
   End If  
Next
' The example displays the following output:
'       Formatting:
'       00:00:03.6974000 ('FFF') --> 697
'       00:00:03.8009000 ('ss\.FFF') --> 03.8
'       
'       Parsing:
'       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
'       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
'       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974");
Console.WriteLine("{0} ('FFF') --> {0:FFF}", ts1);

TimeSpan ts2 = TimeSpan.Parse("0:0:3.8009");
Console.WriteLine("{0} ('ss\\.FFF') --> {0:ss\\.FFF}", ts2);
Console.WriteLine();

Console.WriteLine("Parsing:");
string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279" };
string fmt = @"h\:m\:ss\.FFF";
TimeSpan ts3;

foreach (string input in inputs) {
   if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3);
   else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt);
}
// The example displays the following output:
//       Formatting:
//       00:00:03.6974000 ('FFF') --> 697
//       00:00:03.8009000 ('ss\.FFF') --> 03.8
//       
//       Parsing:
//       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
//       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
//       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.      

返回表首

“FFFF”自定义格式说明符

“FFFF”自定义格式说明符(带有四个“F”字符)输出时间间隔中的万分之几秒。 在格式设置操作中,任何其余的小数位将被截断。 如果小数部分有任何尾随零,则它们不会包含在结果字符串中。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,可以选择显示十分之几秒数、百分之几秒数、千分之几秒数和万分之几秒数。

下面的示例使用“FFFF”自定义格式说明符显示 TimeSpan 值中的万分之几秒。 此外,该示例还在分析操作中使用“FFFF”自定义格式说明符。

Console.WriteLine("Formatting:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.69749")
Console.WriteLine("{0} ('FFFF') --> {0:FFFF}", ts1)

Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.80009")
Console.WriteLine("{0} ('ss\.FFFF') --> {0:ss\.FFFF}", ts2)
Console.WriteLine()

Console.WriteLine("Parsing:")
Dim inputs() As String = { "0:0:03.", "0:0:03.12", "0:0:03.12795" }
Dim fmt As String = "h\:m\:ss\.FFFF"
Dim ts3 As TimeSpan

For Each input As String In inputs
   If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
   Else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt)
   End If  
Next
' The example displays the following output:
'       Formatting:
'       00:00:03.6974900 ('FFFF') --> 6974
'       00:00:03.8000900 ('ss\.FFFF') --> 03.8
'       
'       Parsing:
'       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
'       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
'       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.69749");
Console.WriteLine("{0} ('FFFF') --> {0:FFFF}", ts1);

TimeSpan ts2 = TimeSpan.Parse("0:0:3.80009");
Console.WriteLine("{0} ('ss\\.FFFF') --> {0:ss\\.FFFF}", ts2);
Console.WriteLine();

Console.WriteLine("Parsing:");
string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.12795" };
string fmt = @"h\:m\:ss\.FFFF";
TimeSpan ts3;

foreach (string input in inputs) {
   if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3);
   else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt);
}
// The example displays the following output:
//       Formatting:
//       00:00:03.6974900 ('FFFF') --> 6974
//       00:00:03.8000900 ('ss\.FFFF') --> 03.8
//       
//       Parsing:
//       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
//       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
//       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.

返回表首

“FFFFF”自定义格式说明符

“FFFFF”自定义格式说明符(带有五个“F”字符)输出时间间隔中的十万分之几秒。 在格式设置操作中,任何其余的小数位将被截断。 如果小数部分有任何尾随零,则它们不会包含在结果字符串中。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,可以选择显示十分之几秒数、百分之几秒数、千分之几秒数、万分之几秒数和十万分之几秒数。

下面的示例使用“FFFFF”自定义格式说明符显示 TimeSpan 值中的十万分之几秒。 此外,该示例还在分析操作中使用“FFFFF”自定义格式说明符。

Console.WriteLine("Formatting:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697497")
Console.WriteLine("{0} ('FFFFF') --> {0:FFFFF}", ts1)

Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.800009")
Console.WriteLine("{0} ('ss\.FFFFF') --> {0:ss\.FFFFF}", ts2)
Console.WriteLine()

Console.WriteLine("Parsing:")
Dim inputs() As String = { "0:0:03.", "0:0:03.12", "0:0:03.127956" }
Dim fmt As String = "h\:m\:ss\.FFFFF"
Dim ts3 As TimeSpan

For Each input As String In inputs
   If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
   Else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt)
   End If  
Next
' The example displays the following output:
'       Formatting:
'       00:00:03.6974970 ('FFFFF') --> 69749
'       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
'       
'       Parsing:
'       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
'       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
'       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.697497");
Console.WriteLine("{0} ('FFFFF') --> {0:FFFFF}", ts1);

TimeSpan ts2 = TimeSpan.Parse("0:0:3.800009");
Console.WriteLine("{0} ('ss\\.FFFFF') --> {0:ss\\.FFFFF}", ts2);
Console.WriteLine();

Console.WriteLine("Parsing:");
string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.127956" };
string fmt = @"h\:m\:ss\.FFFFF";
TimeSpan ts3;

foreach (string input in inputs) {
   if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3);
   else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt);
}                       
// The example displays the following output:
//       Formatting:
//       00:00:03.6974970 ('FFFFF') --> 69749
//       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
//       
//       Parsing:
//       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
//       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
//       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.      

返回表首

“FFFFFF”自定义格式说明符

“FFFFFF”自定义格式说明符(带有六个“F”字符)输出时间间隔中的百万分之几秒。 在格式设置操作中,任何其余的小数位将被截断。 如果小数部分有任何尾随零,则它们不会包含在结果字符串中。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,可以选择显示十分之几秒数、百分之几秒数、千分之几秒数、万分之几秒数、十万分之几秒数和百万分之几秒数。

下面的示例使用“FFFFFF”自定义格式说明符显示 TimeSpan 值中的百万分之几秒。 此外,该示例还在分析操作中使用此自定义格式说明符。

Console.WriteLine("Formatting:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
Console.WriteLine("{0} ('FFFFFF') --> {0:FFFFFF}", ts1)

Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8000009")
Console.WriteLine("{0} ('ss\.FFFFFF') --> {0:ss\.FFFFFF}", ts2)
Console.WriteLine()

Console.WriteLine("Parsing:")
Dim inputs() As String = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" }
Dim fmt As String = "h\:m\:ss\.FFFFFF"
Dim ts3 As TimeSpan

For Each input As String In inputs
   If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
   Else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt)
   End If  
Next
' The example displays the following output:
'       Formatting:
'       00:00:03.6974974 ('FFFFFF') --> 697497
'       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
'       
'       Parsing:
'       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
'       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
'       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
Console.WriteLine("{0} ('FFFFFF') --> {0:FFFFFF}", ts1);

TimeSpan ts2 = TimeSpan.Parse("0:0:3.8000009");
Console.WriteLine("{0} ('ss\\.FFFFFF') --> {0:ss\\.FFFFFF}", ts2);
Console.WriteLine();

Console.WriteLine("Parsing:");
string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
string fmt = @"h\:m\:ss\.FFFFFF";
TimeSpan ts3;

foreach (string input in inputs) {
   if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3);
   else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt);
}                       
// The example displays the following output:
//       Formatting:
//       00:00:03.6974974 ('FFFFFF') --> 697497
//       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
//       
//       Parsing:
//       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
//       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
//       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.      

返回表首

“FFFFFFF”自定义格式说明符

“FFFFFFF”自定义格式说明符(带有七个“F”字符)输出时间间隔中的千万分之几秒(或小数嘀嗒数)。 如果小数部分有任何尾随零,则它们不会包含在结果字符串中。 在调用 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的分析操作中,可以选择在输入字符串中存在七个小数位。

下面的示例使用“FFFFFFF”自定义格式说明符显示 TimeSpan 值中秒的小数部分。 此外,该示例还在分析操作中使用此自定义格式说明符。

Console.WriteLine("Formatting:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
Console.WriteLine("{0} ('FFFFFFF') --> {0:FFFFFFF}", ts1)

Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.9500000")
Console.WriteLine("{0} ('ss\.FFFFFFF') --> {0:ss\.FFFFFFF}", ts2)
Console.WriteLine()

Console.WriteLine("Parsing:")
Dim inputs() As String = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" }
Dim fmt As String = "h\:m\:ss\.FFFFFFF"
Dim ts3 As TimeSpan

For Each input As String In inputs
   If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
   Else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt)
   End If  
Next
' The example displays the following output:
'    Formatting:
'    00:00:03.6974974 ('FFFFFFF') --> 6974974
'    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
'    
'    Parsing:
'    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
'    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
'    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569     
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
Console.WriteLine("{0} ('FFFFFFF') --> {0:FFFFFFF}", ts1);

TimeSpan ts2 = TimeSpan.Parse("0:0:3.9500000");
Console.WriteLine("{0} ('ss\\.FFFFFFF') --> {0:ss\\.FFFFFFF}", ts2);
Console.WriteLine();

Console.WriteLine("Parsing:");
string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
string fmt = @"h\:m\:ss\.FFFFFFF";
TimeSpan ts3;

foreach (string input in inputs) {
   if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3);
   else
      Console.WriteLine("Cannot parse {0} with '{1}'.", 
                        input, fmt);
}
// The example displays the following output:
//    Formatting:
//    00:00:03.6974974 ('FFFFFFF') --> 6974974
//    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
//    
//    Parsing:
//    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
//    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
//    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569      

返回表首

其他字符

格式字符串中的任何其他非转义字符(包括空白字符)都被解释为自定义格式说明符。 大多数情况下,存在任何其他非转义字符都会导致引发 FormatException

可以通过两种方式将文本字符包含在格式字符串中:

  • 用单引号将文本字符引起来(文本字符串分隔符)。

  • 在文本字符前面放置一个反斜杠(“\”),它被解释为转义符。 这意味着,在 C# 中,格式字符串必须是用 @ 引起的,或文本字符的前面必须有额外的反斜杠。

.NET Framework 没有为时间间隔中的分隔符定义语法。 这意味着,在格式字符串中,必须将天和小时之间的分隔符、小时和分钟之间的分隔符、分钟和秒之间的分隔符以及秒和秒的小数部分之间的分隔符全都视为字符文本。

下面的示例同时使用转义字符和单引号来定义输出字符串中包含单词“分钟”的自定义格式字符串。

Dim interval As New TimeSpan(0, 32, 45)
' Escape literal characters in a format string.
Dim fmt As String = "mm\:ss\ \m\i\n\u\t\e\s"
Console.WriteLine(interval.ToString(fmt))
' Delimit literal characters in a format string with the ' symbol.
fmt = "mm':'ss' minutes'"
Console.WriteLine(interval.ToString(fmt))
' The example displays the following output: 
'       32:45 minutes      
'       32:45 minutes      
TimeSpan interval = new TimeSpan(0, 32, 45);
// Escape literal characters in a format string.
string fmt = @"mm\:ss\ \m\i\n\u\t\e\s";
Console.WriteLine(interval.ToString(fmt));
// Delimit literal characters in a format string with the ' symbol.
fmt = "mm':'ss' minutes'";      
Console.WriteLine(interval.ToString(fmt));
// The example displays the following output: 
//       32:45 minutes      
//       32:45 minutes      

返回表首

请参见

概念

格式化类型

其他资源

标准 TimeSpan 格式字符串