自訂 TimeSpan 格式字串Custom TimeSpan format strings

TimeSpan 格式字串會定義從格式化作業產生之 TimeSpan 值的字串表示。A TimeSpan format string defines the string representation of a TimeSpan value that results from a formatting operation. 自訂格式字串會由一或多個自訂的 TimeSpan 格式規範搭配任意數目的常值字元所組成。A custom format string consists of one or more custom TimeSpan format specifiers along with any number of literal characters. 任何字串只要不是標準 TimeSpan 格式字串,就會被解譯為自訂的 TimeSpan 格式字串。Any string that isn't a standard TimeSpan format string is interpreted as a custom TimeSpan format string.

重要

自訂 TimeSpan 格式規範不包含預留位置分隔符號,例如分隔天與小時、小時與分鐘或是秒與小數秒的符號。The custom TimeSpan format specifiers don't include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. 相反地,這些符號必須包含在自訂格式字串中作為字串常值。Instead, these symbols must be included in the custom format string as string literals. 例如,"dd\.hh\:mm" 會定義句號 (.) 作為天與小時之間的分隔符號,並定義冒號 (:) 作為小時與分鐘之間的分隔符號。For example, "dd\.hh\:mm" defines a period (.) as the separator between days and hours, and a colon (:) as the separator between hours and minutes.

自訂 TimeSpan 格式規範也不包括讓您區分正負時間間隔的正負號。Custom TimeSpan format specifiers also don't include a sign symbol that enables you to differentiate between negative and positive time intervals. 若要包括正負號,您必須使用條件邏輯建構格式字串。To include a sign symbol, you have to construct a format string by using conditional logic. 其他字元一節中會提供範例。The Other characters section includes an example.

產生 TimeSpan 值的字串表示時,會藉由呼叫 TimeSpan.ToString 方法的多載,以及藉由支援複合格式設定的方法 (例如 String.Format),來產生。The string representations of TimeSpan values are produced by calls to the overloads of the TimeSpan.ToString method, and by methods that support composite formatting, such as String.Format. 如需詳細資訊,請參閱格式化類型複合格式設定For more information, see Formatting Types and Composite Formatting. 下列範例說明如何在格式設定作業中使用自訂格式自串。The following example illustrates the use of custom format strings in formatting operations.

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
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

TimeSpan.ParseExactTimeSpan.TryParseExact 方法也會使用自訂 TimeSpan 格式字串,以定義剖析作業所需的輸入字串格式。Custom TimeSpan format strings are also used by the TimeSpan.ParseExact and TimeSpan.TryParseExact methods to define the required format of input strings for parsing operations. (剖析會將值的字串表示轉換成該值)。下列範例說明剖析作業中標準格式字串的用法。(Parsing converts the string representation of a value to that value.) The following example illustrates the use of standard format strings in parsing operations.

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
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

下表描述自訂日期和時間的格式規範。The following table describes the custom date and time format specifiers.

格式規範Format specifier 說明Description 範例Example
"d"、"%d""d", "%d" 時間間隔中的完整天數。The number of whole days in the time interval.

詳細資訊:"d" 自訂格式規範More information: The "d" custom format specifier.
new TimeSpan(6, 14, 32, 17, 685):

%d --> "6"%d --> "6"

d\.hh\:mm --> "6.14:32"d\.hh\:mm --> "6.14:32"
"dd"-"dddddddd""dd"-"dddddddd" 時間間隔中的完整天數,視需要填補前置零。The number of whole days in the time interval, padded with leading zeros as needed.

詳細資訊:"dd"-"dddddddd" 自訂格式規範More information: The "dd"-"dddddddd" custom format specifiers.
new TimeSpan(6, 14, 32, 17, 685):

ddd --> "006"ddd --> "006"

dd\.hh\:mm --> "06.14:32"dd\.hh\:mm --> "06.14:32"
"h"、"%h""h", "%h" 時間間隔中未計入天數部分的完整時數。The number of whole hours in the time interval that aren't counted as part of days. 單一位數的小時數前面不會加上零。Single-digit hours don't have a leading zero.

詳細資訊:"h" 自訂格式規範More information: The "h" custom format specifier.
new TimeSpan(6, 14, 32, 17, 685):

%h --> "14"%h --> "14"

hh\:mm --> "14:32"hh\:mm --> "14:32"
"hh""hh" 時間間隔中未計入天數部分的完整時數。The number of whole hours in the time interval that aren't counted as part of days. 一位數的小時有前置零。Single-digit hours have a leading zero.

詳細資訊:"hh" 自訂格式規範More information: The "hh" custom format specifier.
new TimeSpan(6, 14, 32, 17, 685):

hh --> "14"hh --> "14"

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

hh --> 08hh --> 08
"m"、"%m""m", "%m" 時間間隔中未納入時數或天數部分的完整分鐘數。The number of whole minutes in the time interval that aren't included as part of hours or days. 單一位數的分鐘數前面不會加上零。Single-digit minutes don't have a leading zero.

詳細資訊:"m" 自訂格式規範More information: The "m" custom format specifier.
new TimeSpan(6, 14, 8, 17, 685):

%m --> "8"%m --> "8"

h\:m --> "14:8"h\:m --> "14:8"
"mm""mm" 時間間隔中未納入時數或天數部分的完整分鐘數。The number of whole minutes in the time interval that aren't included as part of hours or days. 一位數的分鐘有前置零。Single-digit minutes have a leading zero.

詳細資訊:"mm" 自訂格式規範More information: The "mm" custom format specifier.
new TimeSpan(6, 14, 8, 17, 685):

mm --> "08"mm --> "08"

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

d\.hh\:mm\:ss --> 6.08:05:17d\.hh\:mm\:ss --> 6.08:05:17
"s"、"%s""s", "%s" 時間間隔中未納入時數、天數或分鐘數部分的完整秒數。The number of whole seconds in the time interval that aren't included as part of hours, days, or minutes. 單一位數的秒鐘數前面不會加上零。Single-digit seconds don't have a leading zero.

詳細資訊:"s" 自訂格式規範More information: The "s" custom format specifier.
TimeSpan.FromSeconds(12.965)TimeSpan.FromSeconds(12.965):

%s --> 12%s --> 12

s\.fff --> 12.965s\.fff --> 12.965
"ss""ss" 時間間隔中未納入時數、天數或分鐘數部分的完整秒數。The number of whole seconds in the time interval that aren't included as part of hours, days, or minutes. 一位數的秒有前置零。Single-digit seconds have a leading zero.

詳細資訊:"ss" 自訂格式規範More information: The "ss" custom format specifier.
TimeSpan.FromSeconds(6.965)TimeSpan.FromSeconds(6.965):

ss --> 06ss --> 06

ss\.fff --> 06.965ss\.fff --> 06.965
"f"、"%f""f", "%f" 時間間隔中的十分之一秒。The tenths of a second in a time interval.

詳細資訊:"f" 自訂格式規範More information: The "f" custom format specifier.
TimeSpan.FromSeconds(6.895)TimeSpan.FromSeconds(6.895):

f --> 8f --> 8

ss\.f --> 06.8ss\.f --> 06.8
"ff""ff" 時間間隔中的百分之一秒。The hundredths of a second in a time interval.

詳細資訊:"ff" 自訂格式規範More information: The "ff" custom format specifier.
TimeSpan.FromSeconds(6.895)TimeSpan.FromSeconds(6.895):

ff --> 89ff --> 89

ss\.ff --> 06.89ss\.ff --> 06.89
"fff""fff" 時間間隔中的毫秒。The milliseconds in a time interval.

詳細資訊:"fff" 自訂格式規範More information: The "fff" custom format specifier.
TimeSpan.FromSeconds(6.895)TimeSpan.FromSeconds(6.895):

fff --> 895fff --> 895

ss\.fff --> 06.895ss\.fff --> 06.895
"ffff""ffff" 時間間隔中的萬分之一秒。The ten-thousandths of a second in a time interval.

詳細資訊:"ffff" 自訂格式規範More information: The "ffff" custom format specifier.
TimeSpan.Parse("0:0:6.8954321")TimeSpan.Parse("0:0:6.8954321"):

ffff --> 8954ffff --> 8954

ss\.ffff --> 06.8954ss\.ffff --> 06.8954
"fffff""fffff" 時間間隔中的十萬分之一秒。The hundred-thousandths of a second in a time interval.

詳細資訊:"fffff" 自訂格式規範More information: The "fffff" custom format specifier.
TimeSpan.Parse("0:0:6.8954321")TimeSpan.Parse("0:0:6.8954321"):

fffff --> 89543fffff --> 89543

ss\.fffff --> 06.89543ss\.fffff --> 06.89543
"ffffff""ffffff" 時間間隔中的百萬分之一秒。The millionths of a second in a time interval.

詳細資訊:"ffffff" 自訂格式規範More information: The "ffffff" custom format specifier.
TimeSpan.Parse("0:0:6.8954321")TimeSpan.Parse("0:0:6.8954321"):

ffffff --> 895432ffffff --> 895432

ss\.ffffff --> 06.895432ss\.ffffff --> 06.895432
"fffffff""fffffff" 時間間隔中的千萬分之一秒 (或小數刻度)。The ten-millionths of a second (or the fractional ticks) in a time interval.

詳細資訊:"fffffff" 自訂格式規範More information: The "fffffff" custom format specifier.
TimeSpan.Parse("0:0:6.8954321")TimeSpan.Parse("0:0:6.8954321"):

fffffff --> 8954321fffffff --> 8954321

ss\.fffffff --> 06.8954321ss\.fffffff --> 06.8954321
"F"、"%F""F", "%F" 時間間隔中的十分之一秒。The tenths of a second in a time interval. 如果數字為零,則不會顯示任何內容。Nothing is displayed if the digit is zero.

詳細資訊:"F" 自訂格式規範More information: The "F" custom format specifier.
TimeSpan.Parse("00:00:06.32")TimeSpan.Parse("00:00:06.32"):

%F:3%F: 3

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

ss\.F:03.ss\.F: 03.
"FF""FF" 時間間隔中的百分之一秒。The hundredths of a second in a time interval. 不包含小數點後的零或兩位數都是零的數字。Any fractional trailing zeros or two zero digits aren't included.

詳細資訊:"FF" 自訂格式規範More information: The "FF" custom format specifier.
TimeSpan.Parse("00:00:06.329")TimeSpan.Parse("00:00:06.329"):

FF:32FF: 32

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

ss\.FF:03.1ss\.FF: 03.1
"FFF""FFF" 時間間隔中的毫秒。The milliseconds in a time interval. 不包含小數點後的零。Any fractional trailing zeros aren't included.

詳細資訊:More information:
TimeSpan.Parse("00:00:06.3291")TimeSpan.Parse("00:00:06.3291"):

FFF:329FFF: 329

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

ss\.FFF:03.1ss\.FFF: 03.1
"FFFF""FFFF" 時間間隔中的萬分之一秒。The ten-thousandths of a second in a time interval. 不包含小數點後的零。Any fractional trailing zeros aren't included.

詳細資訊:"FFFF" 自訂格式規範More information: The "FFFF" custom format specifier.
TimeSpan.Parse("00:00:06.32917")TimeSpan.Parse("00:00:06.32917"):

FFFFF:3291FFFFF: 3291

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

ss\.FFFF:03.1ss\.FFFF: 03.1
"FFFFF""FFFFF" 時間間隔中的十萬分之一秒。The hundred-thousandths of a second in a time interval. 不包含小數點後的零。Any fractional trailing zeros aren't included.

詳細資訊:"FFFFF" 自訂格式規範More information: The "FFFFF" custom format specifier.
TimeSpan.Parse("00:00:06.329179")TimeSpan.Parse("00:00:06.329179"):

FFFFF:32917FFFFF: 32917

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

ss\.FFFFF:03.1ss\.FFFFF: 03.1
"FFFFFF""FFFFFF" 時間間隔中的百萬分之一秒。The millionths of a second in a time interval. 不顯示小數點後的零。Any fractional trailing zeros aren't displayed.

詳細資訊:"FFFFFF" 自訂格式規範More information: The "FFFFFF" custom format specifier.
TimeSpan.Parse("00:00:06.3291791")TimeSpan.Parse("00:00:06.3291791"):

FFFFFF:329179FFFFFF: 329179

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

ss\.FFFFFF:03.1ss\.FFFFFF: 03.1
"FFFFFFF""FFFFFFF" 時間間隔中的千萬分之一秒。The ten-millions of a second in a time interval. 不顯示小數點後的零或七位數都是零。Any fractional trailing zeros or seven zeros aren't displayed.

詳細資訊:"FFFFFFF" 自訂格式規範More information: The "FFFFFFF" custom format specifier.
TimeSpan.Parse("00:00:06.3291791")TimeSpan.Parse("00:00:06.3291791"):

FFFFFF:3291791FFFFFF: 3291791

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

ss\.FFFFFF:03.19ss\.FFFFFF: 03.19
'string''string' 常值字串分隔符號。Literal string delimiter.

詳細資訊:其他字元More information: Other characters.
new TimeSpan(14, 32, 17):

hh':'mm':'ss --> "14:32:17"hh':'mm':'ss --> "14:32:17"
\\ 逸出字元。The escape character.

詳細資訊:其他字元More information: Other characters.
new TimeSpan(14, 32, 17):

hh\:mm\:ss --> "14:32:17"hh\:mm\:ss --> "14:32:17"
任意字元Any other character 其他任何未逸出字元都會解譯為自訂格式規範。Any other unescaped character is interpreted as a custom format specifier.

詳細資訊:其他字元More Information: Other characters.
new TimeSpan(14, 32, 17):

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

"d" 自訂格式規範The "d" custom format specifier

"d" 自訂格式規範會輸出 TimeSpan.Days 屬性的值,這代表時間間隔中的完整天數。The "d" custom format specifier outputs the value of the TimeSpan.Days property, which represents the number of whole days in the time interval. 它會輸出 TimeSpan 值中的完整天數,即使該值有多個位數也一樣。It outputs the full number of days in a TimeSpan value, even if the value has more than one digit. 如果 TimeSpan.Days屬性的值為零,此規範就會輸出 "0"。If the value of the TimeSpan.Days property is zero, the specifier outputs "0".

如果單獨使用 "d" 自訂格式規範,請指定 "%d",以免被錯誤解譯為標準格式字串。If the "d" custom format specifier is used alone, specify "%d" so that it isn't misinterpreted as a standard format string. 下列範例提供一個實例。The following example provides an illustration.

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

下列範例說明如何使用 "d" 自訂格式規範。The following example illustrates the use of the "d" custom format specifier.

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      
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      

回到表格Back to table

"dd"-"dddddddd" 自訂格式規範The "dd"-"dddddddd" custom format specifiers

"dd"、"ddd"、"dddd"、"ddddd"、"dddddd"、"ddddddd" 和 "dddddddd" 自訂格式規範會輸出 TimeSpan.Days 屬性的值,這代表時間間隔中的完整天數。The "dd", "ddd", "dddd", "ddddd", "dddddd", "ddddddd", and "dddddddd" custom format specifiers output the value of the TimeSpan.Days property, which represents the number of whole days in the time interval.

輸出字串會包含格式規範中 "d" 字元數所指定的最少位數,並且視需要填補前置零。The output string includes a minimum number of digits specified by the number of "d" characters in the format specifier, and it's padded with leading zeros as needed. 如果天數位數超過格式規範中的 "d" 字元數,則結果字串中的輸出會是完整天數。If the digits in the number of days exceed the number of "d" characters in the format specifier, the full number of days is output in the result string.

下列範例會使用這些格式規範,以顯示兩個 TimeSpan 值的字串表示。The following example uses these format specifiers to display the string representation of two TimeSpan values. 第一個時間間隔的天數部分值為零,第二個時間間隔的天數部分值為 365。The value of the days component of the first time interval is zero; the value of the days component of the second is 365.

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      
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      

回到表格Back to table

"h" 自訂格式規範The "h" custom format specifier

"h" 自訂格式規範會輸出 TimeSpan.Hours 屬性的值,這代表時間間隔中未計入天數部分的完整時數。The "h" custom format specifier outputs the value of the TimeSpan.Hours property, which represents the number of whole hours in the time interval that isn't counted as part of its day component. 如果 TimeSpan.Hours 屬性的值為 0 到 9,就會傳回一位數的字串值;如果 TimeSpan.Hours 屬性的值為 10 到 23,則會傳回兩位數的字串值。It returns a one-digit string value if the value of the TimeSpan.Hours property is 0 through 9, and it returns a two-digit string value if the value of the TimeSpan.Hours property ranges from 10 to 23.

如果單獨使用 "h" 自訂格式規範,請指定 "%h",以免被錯誤解譯為標準格式字串。If the "h" custom format specifier is used alone, specify "%h" so that it isn't misinterpreted as a standard format string. 下列範例提供一個實例。The following example provides an illustration.

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
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

通常在剖析作業中,只包含單一數字的輸入字串會解譯為天數。Ordinarily, in a parsing operation, an input string that includes only a single number is interpreted as the number of days. 您可以改用 "%h" 自訂格式規範會,將數值字串解譯為時數。You can use the "%h" custom format specifier instead to interpret the numeric string as the number of hours. 下列範例提供一個實例。The following example provides an illustration.

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                              
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                              

下列範例說明如何使用 "h" 自訂格式規範。The following example illustrates the use of the "h" custom format specifier.

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
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

回到表格Back to table

"hh" 自訂格式規範The "hh" custom format specifier

"hh" 自訂格式規範會輸出 TimeSpan.Hours 屬性的值,這代表時間間隔中未計入天數部分的完整時數。The "hh" custom format specifier outputs the value of the TimeSpan.Hours property, which represents the number of whole hours in the time interval that isn't counted as part of its day component. 對於從 0 到 9 的值,輸出字串會包含前置零。For values from 0 through 9, the output string includes a leading zero.

通常在剖析作業中,只包含單一數字的輸入字串會解譯為天數。Ordinarily, in a parsing operation, an input string that includes only a single number is interpreted as the number of days. 您可以改用 "hh" 自訂格式規範,將數值字串解譯為時數。You can use the "hh" custom format specifier instead to interpret the numeric string as the number of hours. 下列範例提供一個實例。The following example provides an illustration.

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                              
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                              

下列範例說明如何使用 "hh" 自訂格式規範。The following example illustrates the use of the "hh" custom format specifier.

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
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

回到表格Back to table

"m" 自訂格式規範The "m" custom format specifier

"m" 自訂格式規範會輸出 TimeSpan.Minutes 屬性的值,這代表時間間隔中未計入天數部分的完整分鐘數。The "m" custom format specifier outputs the value of the TimeSpan.Minutes property, which represents the number of whole minutes in the time interval that isn't counted as part of its day component. 如果 TimeSpan.Minutes 屬性的值為 0 到 9,就會傳回一位數的字串值;如果 TimeSpan.Minutes 屬性的值為 10 到 59,則會傳回兩位數的字串值。It returns a one-digit string value if the value of the TimeSpan.Minutes property is 0 through 9, and it returns a two-digit string value if the value of the TimeSpan.Minutes property ranges from 10 to 59.

如果單獨使用 "m" 自訂格式規範,請指定 "%m",以免被錯誤解譯為標準格式字串。If the "m" custom format specifier is used alone, specify "%m" so that it isn't misinterpreted as a standard format string. 下列範例提供一個實例。The following example provides an illustration.

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
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

通常在剖析作業中,只包含單一數字的輸入字串會解譯為天數。Ordinarily, in a parsing operation, an input string that includes only a single number is interpreted as the number of days. 您可以改用 "%m" 自訂格式規範,將數值字串解譯為分鐘數。You can use the "%m" custom format specifier instead to interpret the numeric string as the number of minutes. 下列範例提供一個實例。The following example provides an illustration.

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                              
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                              

下列範例說明如何使用 "m" 自訂格式規範。The following example illustrates the use of the "m" custom format specifier.

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
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

回到表格Back to table

"mm" 自訂格式規範The "mm" custom format specifier

"mm" 自訂格式規範會輸出 TimeSpan.Minutes 屬性的值,這代表時間間隔中未包含在時數或天數部分中的完整分鐘數。The "mm" custom format specifier outputs the value of the TimeSpan.Minutes property, which represents the number of whole minutes in the time interval that isn't included as part of its hours or days component. 對於從 0 到 9 的值,輸出字串會包含前置零。For values from 0 through 9, the output string includes a leading zero.

通常在剖析作業中,只包含單一數字的輸入字串會解譯為天數。Ordinarily, in a parsing operation, an input string that includes only a single number is interpreted as the number of days. 您可以改用 "mm" 自訂格式規範,將數值字串解譯為分鐘數。You can use the "mm" custom format specifier instead to interpret the numeric string as the number of minutes. 下列範例提供一個實例。The following example provides an illustration.

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                              
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           

下列範例說明如何使用 "mm" 自訂格式規範。The following example illustrates the use of the "mm" custom format specifier.

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      
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      

回到表格Back to table

"s" 自訂格式規範The "s" custom format specifier

"s" 自訂格式規範會輸出 TimeSpan.Seconds 屬性的值,這代表時間間隔中未包含在時數、天數或分鐘數部分中的完整秒數。The "s" custom format specifier outputs the value of the TimeSpan.Seconds property, which represents the number of whole seconds in the time interval that isn't included as part of its hours, days, or minutes component. 如果 TimeSpan.Seconds 屬性的值為 0 到 9,就會傳回一位數的字串值;如果 TimeSpan.Seconds 屬性的值為 10 到 59,則會傳回兩位數的字串值。It returns a one-digit string value if the value of the TimeSpan.Seconds property is 0 through 9, and it returns a two-digit string value if the value of the TimeSpan.Seconds property ranges from 10 to 59.

如果單獨使用 "s" 自訂格式規範,請指定 "%s",以免被錯誤解譯為標準格式字串。If the "s" custom format specifier is used alone, specify "%s" so that it isn't misinterpreted as a standard format string. 下列範例提供一個實例。The following example provides an illustration.

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

通常在剖析作業中,只包含單一數字的輸入字串會解譯為天數。Ordinarily, in a parsing operation, an input string that includes only a single number is interpreted as the number of days. 您可以改用 "%s" 自訂格式規範,將數值字串解譯為秒數。You can use the "%s" custom format specifier instead to interpret the numeric string as the number of seconds. 下列範例提供一個實例。The following example provides an illustration.

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
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

下列範例說明如何使用 "s" 自訂格式規範。The following example illustrates the use of the "s" custom format specifier.

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      
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      

回到表格Back to table

"ss" 自訂格式規範The "ss" custom format specifier

"ss" 自訂格式規範會輸出 TimeSpan.Seconds 屬性的值,這代表時間間隔中未包含在時數、天數或分鐘數部分中的完整秒數。The "ss" custom format specifier outputs the value of the TimeSpan.Seconds property, which represents the number of whole seconds in the time interval that isn't included as part of its hours, days, or minutes component. 對於從 0 到 9 的值,輸出字串會包含前置零。For values from 0 through 9, the output string includes a leading zero.

通常在剖析作業中,只包含單一數字的輸入字串會解譯為天數。Ordinarily, in a parsing operation, an input string that includes only a single number is interpreted as the number of days. 您可以改用 "ss" 自訂格式規範,將數值字串解譯為秒數。You can use the "ss" custom format specifier instead to interpret the numeric string as the number of seconds. 下列範例提供一個實例。The following example provides an illustration.

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
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

下列範例說明如何使用 "ss" 自訂格式規範。The following example illustrates the use of the "ss" custom format specifier.

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
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

回到表格Back to table

"f" 自訂格式規範The "f" custom format specifier

"f" 自訂格式規範會輸出時間間隔中的十分之一秒。The "f" custom format specifier outputs the tenths of a second in a time interval. 在格式化作業中,會截斷其餘任何小數位數。In a formatting operation, any remaining fractional digits are truncated. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,輸入字串必須確切包含一個小數位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the input string must contain exactly one fractional digit.

如果單獨使用 "f" 自訂格式規範,請指定 "%f",以免被錯誤解譯為標準格式字串。If the "f" custom format specifier is used alone, specify "%f" so that it isn't misinterpreted as a standard format string.

下列範例會使用 "f" 自訂格式規範,以顯示 TimeSpan 值中的十分之一秒。The following example uses the "f" custom format specifier to display the tenths of a second in a TimeSpan value. 首先會使用 "f" 作為唯一的格式規範,然後再放在自訂格式字串中與 "s" 規範結合。"f" is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

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      
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

回到表格Back to table

"ff" 自訂格式規範The "ff" custom format specifier

"ff" 自訂格式規範會輸出時間間隔中的百分之一秒。The "ff" custom format specifier outputs the hundredths of a second in a time interval. 在格式化作業中,會截斷其餘任何小數位數。In a formatting operation, any remaining fractional digits are truncated. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,輸入字串必須確切包含兩個小數位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the input string must contain exactly two fractional digits.

下列範例會使用 "ff" 自訂格式規範,以顯示 TimeSpan 值中的百分之一秒。The following example uses the "ff" custom format specifier to display the hundredths of a second in a TimeSpan value. 首先會使用 "ff" 作為唯一的格式規範,然後再放在自訂格式字串中與 "s" 規範結合。"ff" is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

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      
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

回到表格Back to table

"fff" 自訂格式規範The "fff" custom format specifier

"fff" 自訂格式規範 (有三個 "f" 字元) 會輸出時間間隔中的毫秒。The "fff" custom format specifier (with three "f" characters) outputs the milliseconds in a time interval. 在格式化作業中,會截斷其餘任何小數位數。In a formatting operation, any remaining fractional digits are truncated. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,輸入字串必須確切包含三個小數位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the input string must contain exactly three fractional digits.

下列範例會使用 "fff" 自訂格式規範,以顯示 TimeSpan 值中的毫秒。The following example uses the "fff" custom format specifier to display the milliseconds in a TimeSpan value. 首先會使用 "fff" 作為唯一的格式規範,然後再放在自訂格式字串中與 "s" 規範結合。"fff" is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

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      
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

回到表格Back to table

"ffff" 自訂格式規範The "ffff" custom format specifier

"ffff" 自訂格式規範 (有四個 "f" 字元) 會輸出時間間隔中的萬分之一秒。The "ffff" custom format specifier (with four "f" characters) outputs the ten-thousandths of a second in a time interval. 在格式化作業中,會截斷其餘任何小數位數。In a formatting operation, any remaining fractional digits are truncated. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,輸入字串必須確切包含四個小數位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the input string must contain exactly four fractional digits.

下列範例會使用 "ffff" 自訂格式規範,以顯示 TimeSpan 值中的萬分之一秒。The following example uses the "ffff" custom format specifier to display the ten-thousandths of a second in a TimeSpan value. 首先會使用 "ffff" 作為唯一的格式規範,然後再放在自訂格式字串中與 "s" 規範結合。"ffff" is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

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      
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

回到表格Back to table

"fffff" 自訂格式規範The "fffff" custom format specifier

"fffff" 自訂格式規範 (有五個 "f" 字元) 會輸出時間間隔中的十萬分之一秒。The "fffff" custom format specifier (with five "f" characters) outputs the hundred-thousandths of a second in a time interval. 在格式化作業中,會截斷其餘任何小數位數。In a formatting operation, any remaining fractional digits are truncated. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,輸入字串必須確切包含五個小數位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the input string must contain exactly five fractional digits.

下列範例會使用 "fffff" 自訂格式規範,以顯示 TimeSpan 值中的十萬分之一秒。The following example uses the "fffff" custom format specifier to display the hundred-thousandths of a second in a TimeSpan value. 首先會使用 "fffff" 作為唯一的格式規範,然後再放在自訂格式字串中與 "s" 規範結合。"fffff" is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

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      
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

回到表格Back to table

"ffffff" 自訂格式規範The "ffffff" custom format specifier

"ffffff" 自訂格式規範 (有六個 "f" 字元) 會輸出時間間隔中的百萬分之一秒。The "ffffff" custom format specifier (with six "f" characters) outputs the millionths of a second in a time interval. 在格式化作業中,會截斷其餘任何小數位數。In a formatting operation, any remaining fractional digits are truncated. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,輸入字串必須確切包含六個小數位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the input string must contain exactly six fractional digits.

下列範例會使用 "ffffff" 自訂格式規範,以顯示 TimeSpan 值中的百萬分之一秒。The following example uses the "ffffff" custom format specifier to display the millionths of a second in a TimeSpan value. 首先會使用它作為唯一的格式規範,然後再放在自訂格式字串中與 "s" 規範結合。It is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

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      
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

回到表格Back to table

"fffffff" 自訂格式規範The "fffffff" custom format specifier

"fffffff" 自訂格式規範 (有七個 "f" 字元) 會輸出時間間隔中的千萬分之一秒 (或刻度的小數值)。The "fffffff" custom format specifier (with seven "f" characters) outputs the ten-millionths of a second (or the fractional number of ticks) in a time interval. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,輸入字串必須確切包含七個小數位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the input string must contain exactly seven fractional digits.

下列範例會使用 "fffffff" 自訂格式規範,以顯示 TimeSpan 值中刻度的小數值。The following example uses the "fffffff" custom format specifier to display the fractional number of ticks in a TimeSpan value. 首先會使用它作為唯一的格式規範,然後再放在自訂格式字串中與 "s" 規範結合。It is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

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      
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

回到表格Back to table

"F" 自訂格式規範The "F" custom format specifier

"F" 自訂格式規範會輸出時間間隔中的十分之一秒。The "F" custom format specifier outputs the tenths of a second in a time interval. 在格式化作業中,會截斷其餘任何小數位數。In a formatting operation, any remaining fractional digits are truncated. 如果時間間隔十分之一秒的值為零,它就不會包含在結果字串中。If the value of the time interval's tenths of a second is zero, it isn't included in the result string. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,可以視需要使用十分之一秒的位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the presence of the tenths of a second digit is optional.

如果單獨使用 "F" 自訂格式規範,請指定 "%F",以免被錯誤解譯為標準格式字串。If the "F" custom format specifier is used alone, specify "%F" so that it isn't misinterpreted as a standard format string.

下列範例會使用 "F" 自訂格式規範,以顯示 TimeSpan 值中的十分之一秒。The following example uses the "F" custom format specifier to display the tenths of a second in a TimeSpan value. 它也會在剖析作業中使用此自訂格式規範。It also uses this custom format specifier in a parsing operation.

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'.      
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'.      

回到表格Back to table

"FF" 自訂格式規範The "FF" custom format specifier

"FF" 自訂格式規範會輸出時間間隔中的百分之一秒。The "FF" custom format specifier outputs the hundredths of a second in a time interval. 在格式化作業中,會截斷其餘任何小數位數。In a formatting operation, any remaining fractional digits are truncated. 如果小數點後有任何零,這些零不會包含在結果字串中。If there are any trailing fractional zeros, they aren't included in the result string. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,可以視需要使用十分之一秒和百分之一秒的位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the presence of the tenths and hundredths of a second digit is optional.

下列範例會使用 "FF" 自訂格式規範,以顯示 TimeSpan 值中的百分之一秒。The following example uses the "FF" custom format specifier to display the hundredths of a second in a TimeSpan value. 它也會在剖析作業中使用此自訂格式規範。It also uses this custom format specifier in a parsing operation.

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'.      
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'.

回到表格Back to table

"FFF" 自訂格式規範The "FFF" custom format specifier

"FFF" 自訂格式規範 (有三個 "F" 字元) 會輸出時間間隔中的毫秒。The "FFF" custom format specifier (with three "F" characters) outputs the milliseconds in a time interval. 在格式化作業中,會截斷其餘任何小數位數。In a formatting operation, any remaining fractional digits are truncated. 如果小數點後有任何零,這些零不會包含在結果字串中。If there are any trailing fractional zeros, they aren't included in the result string. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,可以視需要使用十分之一秒、百分之一秒及千分之一秒的位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the presence of the tenths, hundredths, and thousandths of a second digit is optional.

下列範例會使用 "FFF" 自訂格式規範,以顯示 TimeSpan 值中的千分之一秒。The following example uses the "FFF" custom format specifier to display the thousandths of a second in a TimeSpan value. 它也會在剖析作業中使用此自訂格式規範。It also uses this custom format specifier in a parsing operation.

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'.      
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'.

回到表格Back to table

"FFFF" 自訂格式規範The "FFFF" custom format specifier

"FFFF" 自訂格式規範 (有四個 "F" 字元) 會輸出時間間隔中的萬分之一秒。The "FFFF" custom format specifier (with four "F" characters) outputs the ten-thousandths of a second in a time interval. 在格式化作業中,會截斷其餘任何小數位數。In a formatting operation, any remaining fractional digits are truncated. 如果小數點後有任何零,這些零不會包含在結果字串中。If there are any trailing fractional zeros, they aren't included in the result string. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,可以視需要使用十分之一秒、百分之一秒、千分之一秒及萬分之一秒的位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the presence of the tenths, hundredths, thousandths, and ten-thousandths of a second digit is optional.

下列範例會使用 "FFFF" 自訂格式規範,以顯示 TimeSpan 值中的萬分之一秒。The following example uses the "FFFF" custom format specifier to display the ten-thousandths of a second in a TimeSpan value. 它也會在剖析作業中使用 "FFFF" 自訂格式規範。It also uses the "FFFF" custom format specifier in a parsing operation.

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'.
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'.

回到表格Back to table

"FFFFF" 自訂格式規範The "FFFFF" custom format specifier

"FFFFF" 自訂格式規範 (有五個 "F" 字元) 會輸出時間間隔中的十萬分之一秒。The "FFFFF" custom format specifier (with five "F" characters) outputs the hundred-thousandths of a second in a time interval. 在格式化作業中,會截斷其餘任何小數位數。In a formatting operation, any remaining fractional digits are truncated. 如果小數點後有任何零,這些零不會包含在結果字串中。If there are any trailing fractional zeros, they aren't included in the result string. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,可以視需要使用十分之一秒、百分之一秒、千分之一秒、萬分之一秒及十萬分之一秒的位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the presence of the tenths, hundredths, thousandths, ten-thousandths, and hundred-thousandths of a second digit is optional.

下列範例會使用 "FFFFF" 自訂格式規範,以顯示 TimeSpan 值中的十萬分之一秒。The following example uses the "FFFFF" custom format specifier to display the hundred-thousandths of a second in a TimeSpan value. 它也會在剖析作業中使用 "FFFFF" 自訂格式規範。It also uses the "FFFFF" custom format specifier in a parsing operation.

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'.      
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'.

回到表格Back to table

"FFFFFF" 自訂格式規範The "FFFFFF" custom format specifier

"FFFFFF" 自訂格式規範 (有六個 "F" 字元) 會輸出時間間隔中的百萬分之一秒。The "FFFFFF" custom format specifier (with six "F" characters) outputs the millionths of a second in a time interval. 在格式化作業中,會截斷其餘任何小數位數。In a formatting operation, any remaining fractional digits are truncated. 如果小數點後有任何零,這些零不會包含在結果字串中。If there are any trailing fractional zeros, they aren't included in the result string. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,可以視需要使用十分之一秒、百分之一秒、千分之一秒、萬分之一秒、十萬分之一秒及百萬分之一秒的位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the presence of the tenths, hundredths, thousandths, ten-thousandths, hundred-thousandths, and millionths of a second digit is optional.

下列範例會使用 "FFFFFF" 自訂格式規範,以顯示 TimeSpan 值中的百萬分之一秒。The following example uses the "FFFFFF" custom format specifier to display the millionths of a second in a TimeSpan value. 它也會在剖析作業中使用此自訂格式規範。It also uses this custom format specifier in a parsing operation.

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'.      
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'.

回到表格Back to table

"FFFFFFF" 自訂格式規範The "FFFFFFF" custom format specifier

"FFFFFFF" 自訂格式規範 (有七個 "F" 字元) 會輸出時間間隔中的千萬分之一秒 (或刻度的小數值)。The "FFFFFFF" custom format specifier (with seven "F" characters) outputs the ten-millionths of a second (or the fractional number of ticks) in a time interval. 如果小數點後有任何零,這些零不會包含在結果字串中。If there are any trailing fractional zeros, they aren't included in the result string. 在呼叫 TimeSpan.ParseExactTimeSpan.TryParseExact 方法的剖析作業中,可以視需要在輸入字串中使用七個小數位數。In a parsing operation that calls the TimeSpan.ParseExact or TimeSpan.TryParseExact method, the presence of the seven fractional digits in the input string is optional.

下列範例會使用 "FFFFFFF" 自訂格式規範,以顯示 TimeSpan 值中的小數秒部分。The following example uses the "FFFFFFF" custom format specifier to display the fractional parts of a second in a TimeSpan value. 它也會在剖析作業中使用此自訂格式規範。It also uses this custom format specifier in a parsing operation.

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      
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     

回到表格Back to table

其他字元Other characters

格式字串中任何其他未逸出的字元 (包括空白字元),都會解譯為自訂格式規範。Any other unescaped character in a format string, including a white-space character, is interpreted as a custom format specifier. 在大多數情況下,其他任何未逸出的字元都會導致 FormatExceptionIn most cases, the presence of any other unescaped character results in a FormatException.

有兩個方式可以在格式字串中納入常值字元:There are two ways to include a literal character in a format string:

  • 用單引號 (常值字串分隔符號) 括住字元。Enclose it in single quotation marks (the literal string delimiter).

  • 在字元前面加上反斜線 ("\"),這樣就會解譯為逸出字元。Precede it with a backslash ("\"), which is interpreted as an escape character. 亦即在 C# 中,格式字串必須括以 @-quoted,或必須在常值字元前額外加上一道反斜線。This means that, in C#, the format string must either be @-quoted, or the literal character must be preceded by an additional backslash.

    在某些情況下,您可能需要使用條件邏輯,才能在格式字串中包含逸出的常值。In some cases, you may have to use conditional logic to include an escaped literal in a format string. 下列範例將使用條件邏輯包含代表負時間間隔的正負號。The following example uses conditional logic to include a sign symbol for negative time intervals.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          TimeSpan result = new DateTime(2010, 01, 01) - DateTime.Now; 
          String fmt = (result < TimeSpan.Zero ?  "\\-" : "") + "dd\\.hh\\:mm";
          
          Console.WriteLine(result.ToString(fmt));
          Console.WriteLine("Interval: {0:" + fmt + "}", result);
       }
    }
    // The example displays output like the following:
    //       -1291.10:54
    //       Interval: -1291.10:54
    
    Module Example
       Public Sub Main()
          Dim result As TimeSpan = New DateTime(2010, 01, 01) - Date.Now 
          Dim fmt As String = If(result < TimeSpan.Zero, "\-", "") + "dd\.hh\:mm"
          
          Console.WriteLine(result.ToString(fmt))
          Console.WriteLine("Interval: {0:" + fmt + "}", result)
       End Sub
    End Module
    ' The example displays output like the following:
    '       -1291.10:54
    '       Interval: -1291.10:54
    

.NET 不會定義時間間隔中分隔符號的文法。.NET doesn't define a grammar for separators in time intervals. 這表示天與小時、小時與分鐘、分鐘與秒以及秒與小數秒之間的分隔符號,都必須以格式字串中的字元常值方式處理。This means that the separators between days and hours, hours and minutes, minutes and seconds, and seconds and fractions of a second must all be treated as character literals in a format string.

下列範例使用逸出字元和單引號,定義會在輸出字串中加上 "minutes" 一詞的自訂格式字串。The following example uses both the escape character and the single quote to define a custom format string that includes the word "minutes" in the output string.

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      
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      

回到表格Back to table

另請參閱See also