標準 TimeSpan 格式字串Standard TimeSpan Format Strings

標準 TimeSpan 格式字串會使用單一格式規範,來定義從格式化作業所產生之 TimeSpan 值的文字表示。A standard TimeSpan format string uses a single format specifier to define the text representation of a TimeSpan value that results from a formatting operation. 任何包含一個以上字元 (包含空格) 的格式字串,都會解譯為自訂 TimeSpan 格式字串。Any format string that contains more than one character, including white space, is interpreted as a custom TimeSpan format string. 如需詳細資訊,請參閱自訂 TimeSpan 格式字串For more information, see Custom TimeSpan Format Strings .

TimeSpan 值的字串表示,藉由呼叫 TimeSpan.ToString 方法的多載而產生,同時也可藉由支援複合格式化的方法所產生,例如 String.FormatThe string representations of TimeSpan values are produced by calls to the overloads of the TimeSpan.ToString method, as well as 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 standard format strings in formatting operations.

using System;

public class Example
{
   public static void Main()
   {
      TimeSpan duration = new TimeSpan(1, 12, 23, 62);
      string output = "Time of Travel: " + duration.ToString("c");
      Console.WriteLine(output);
      
      Console.WriteLine("Time of Travel: {0:c}", duration); 
   }
}
// The example displays the following output:
//       Time of Travel: 1.12:24:02
//       Time of Travel: 1.12:24:02
Module Example
   Public Sub Main()
      Dim duration As New TimeSpan(1, 12, 23, 62)
      Dim output As String = "Time of Travel: " + duration.ToString("c")
      Console.WriteLine(output)
      
      Console.WriteLine("Time of Travel: {0:c}", duration) 
   End Sub
End Module
' The example displays the following output:
'       Time of Travel: 1.12:24:02
'       Time of Travel: 1.12:24:02

TimeSpanTimeSpan.ParseExact 方法也會使用標準 TimeSpan.TryParseExact 格式字串,以定義剖析作業之輸入字串的必要格式Standard 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 = "1.03:14:56.1667";
      TimeSpan interval;
      try {
         interval = TimeSpan.ParseExact(value, "c", null);
         Console.WriteLine("Converted '{0}' to {1}", value, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("{0}: Bad Format", value);
      }   
      catch (OverflowException) {
         Console.WriteLine("{0}: Out of Range", value);
      }
      
      if (TimeSpan.TryParseExact(value, "c", null, out interval))
         Console.WriteLine("Converted '{0}' to {1}", value, interval);
      else
         Console.WriteLine("Unable to convert {0} to a time interval.", 
                           value);
   }
}
// The example displays the following output:
//       Converted '1.03:14:56.1667' to 1.03:14:56.1667000
//       Converted '1.03:14:56.1667' to 1.03:14:56.1667000
Module Example
   Public Sub Main()
      Dim value As String = "1.03:14:56.1667"
      Dim interval As TimeSpan
      Try
         interval = TimeSpan.ParseExact(value, "c", Nothing)
         Console.WriteLine("Converted '{0}' to {1}", value, interval)
      Catch e As FormatException
         Console.WriteLine("{0}: Bad Format", value)
      Catch e As OverflowException
         Console.WriteLine("{0}: Out of Range", value)
      End Try
      
      If TimeSpan.TryParseExact(value, "c", Nothing, interval) Then
         Console.WriteLine("Converted '{0}' to {1}", value, interval)
      Else
         Console.WriteLine("Unable to convert {0} to a time interval.", 
                           value)
      End If                
   End Sub
End Module
' The example displays the following output:
'       Converted '1.03:14:56.1667' to 1.03:14:56.1667000
'       Converted '1.03:14:56.1667' to 1.03:14:56.1667000

下表列出標準時間間隔格式規範。The following table lists the standard time interval format specifiers.

格式規範Format specifier 名稱Name 說明Description 範例Examples
"c""c" 常數 (非變異) 格式Constant (invariant) format 這個規範不區分文化特性。This specifier is not culture-sensitive. 它採用 [-][d'.']hh':'mm':'ss['.'fffffff] 格式It takes the form [-][d'.']hh':'mm':'ss['.'fffffff].

\ ("t" 與 "T" 格式字串會產生相同的結果)。(The "t" and "T" format strings produce the same results.)

詳細資訊:常數 ("c") 格式規範More information: The Constant ("c") Format Specifier.
TimeSpan.Zero -> 00:00:00TimeSpan.Zero -> 00:00:00

New TimeSpan(0, 0, 30, 0) -> 00:30:00New TimeSpan(0, 0, 30, 0) -> 00:30:00

New TimeSpan(3, 17, 25, 30, 500) -> 3.17:25:30.5000000New TimeSpan(3, 17, 25, 30, 500) -> 3.17:25:30.5000000
"g""g" 一般短格式General short format 這個規範只會輸出需要的內容。This specifier outputs only what is needed. 它會區分文化特性,並採用 [-][d':']h':'mm':'ss[.FFFFFFF] 格式。It is culture-sensitive and takes the form [-][d':']h':'mm':'ss[.FFFFFFF].

詳細資訊:一般短 ("g") 格式規範More information: The General Short ("g") Format Specifier.
New TimeSpan(1, 3, 16, 50, 500) -> 1:3:16:50.5 (en-US)New TimeSpan(1, 3, 16, 50, 500) -> 1:3:16:50.5 (en-US)

New TimeSpan(1, 3, 16, 50, 500) -> 1:3:16:50,5 (fr-FR)New TimeSpan(1, 3, 16, 50, 500) -> 1:3:16:50,5 (fr-FR)

New TimeSpan(1, 3, 16, 50, 599) -> 1:3:16:500.599 (en-US)New TimeSpan(1, 3, 16, 50, 599) -> 1:3:16:50.599 (en-US)

New TimeSpan(1, 3, 16, 50, 599) -> 1:3:16:500.599 (fr-FR)New TimeSpan(1, 3, 16, 50, 599) -> 1:3:16:50,599 (fr-FR)
"G""G" 一般長格式General long format 這個規範一律會輸出天數和七個小數。This specifier always outputs days and seven fractional digits. 它會區分文化特性,並採用 [-]d':'hh':'mm':'ss.fffffff 格式。It is culture-sensitive and takes the form [-]d':'hh':'mm':'ss.fffffff.

詳細資訊:一般長 ("G") 格式規範More information: The General Long ("G") Format Specifier.
New TimeSpan(18, 30, 0) -> 0:18:30:00.0000000 (en-US)New TimeSpan(18, 30, 0) -> 0:18:30:00.0000000 (en-US)

New TimeSpan(18, 30, 0) -> 0:18:30:00,0000000 (fr-FR)New TimeSpan(18, 30, 0) -> 0:18:30:00,0000000 (fr-FR)

常數 ("c") 格式規範The Constant ("c") Format Specifier

"c" 格式規範會以下列形式傳回 TimeSpan 值的字串表示:The "c" format specifier returns the string representation of a TimeSpan value in the following form:

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

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 句號 (.) 和冒號 (:) 是常值的符號。The period (.) and colon (:) are literal symbols. 下表說明其餘項目。The following table describes the remaining elements.

元素Element 說明Description
- 選擇性的負號,表示負的時間間隔。An optional negative sign, which indicates a negative time interval.
dd 選擇性的天數,沒有前置的零。The optional number of days, with no leading zeros.
hhhh 小時數,範圍從 "00" 到 "23"。The number of hours, which ranges from "00" to "23".
mmmm 分鐘數,範圍從 "00" 到 "59"。The number of minutes, which ranges from "00" to "59".
ssss 秒數,範圍從 "0" 到 "59"。The number of seconds, which ranges from "0" to "59".
ffffffffffffff 秒的選擇性小數部分。The optional fractional portion of a second. 其值的範圍可從 "0000001" (一個刻度或一秒的千萬分之一) 到 "9999999" (一秒的千萬分之 9,999,999,也就是一秒減一個刻度)。Its value can range from "0000001" (one tick, or one ten-millionth of a second) to "9999999" (9,999,999 ten-millionths of a second, or one second less one tick).

與 "g" 和 "G" 格式規範不同,"c" 格式規範不區分文化特性。Unlike the "g" and "G" format specifiers, the "c" format specifier is not culture-sensitive. 它會產生 TimeSpan 值的字串表示法,該值是非變異值,且對於 .NET Framework 4 之前的所有舊版 .NET Framework 皆通用。It produces the string representation of a TimeSpan value that is invariant and that is common to all previous versions of the .NET Framework before the .NET Framework 4. "c" 是預設的 TimeSpan 格式字串;TimeSpan.ToString() 方法使用 "c" 格式字串來將時間間隔值格式化。"c" is the default TimeSpan format string; the TimeSpan.ToString() method formats a time interval value by using the "c" format string.

注意

TimeSpan 也支援 "t" 和 "T" 標準格式字串,它們的行為與 "c" 標準格式字串相同。TimeSpan also supports the "t" and "T" standard format strings, which are identical in behavior to the "c" standard format string.

下列範例會呈現兩個 TimeSpan 物件,用以執行算術運算,並顯示結果。The following example instantiates two TimeSpan objects, uses them to perform arithmetic operations, and displays the result. 在每個案例中,都使用複合格式化,以 "c" 格式規範顯示 TimeSpan 值。In each case, it uses composite formatting to display the TimeSpan value by using the "c" format specifier.

using System;

public class Example
{
   public static void Main()
   {
      TimeSpan interval1, interval2;
      interval1 = new TimeSpan(7, 45, 16);
      interval2 = new TimeSpan(18, 12, 38);
      
      Console.WriteLine("{0:c} - {1:c} = {2:c}", interval1, 
                        interval2, interval1 - interval2);
      Console.WriteLine("{0:c} + {1:c} = {2:c}", interval1, 
                        interval2, interval1 + interval2);
      
      interval1 = new TimeSpan(0, 0, 1, 14, 365);
      interval2 = TimeSpan.FromTicks(2143756);  
      Console.WriteLine("{0:c} + {1:c} = {2:c}", interval1, 
                        interval2, interval1 + interval2);
   }
}
// The example displays the following output:
//       07:45:16 - 18:12:38 = -10:27:22
//       07:45:16 + 18:12:38 = 1.01:57:54
//       00:01:14.3650000 + 00:00:00.2143756 = 00:01:14.5793756
Module Example
   Public Sub Main()
      Dim interval1, interval2 As TimeSpan
      interval1 = New TimeSpan(7, 45, 16)
      interval2 = New TimeSpan(18, 12, 38)
      
      Console.WriteLine("{0:c} - {1:c} = {2:c}", interval1, 
                        interval2, interval1 - interval2)
      Console.WriteLine("{0:c} + {1:c} = {2:c}", interval1, 
                        interval2, interval1 + interval2)
      
      interval1 = New TimeSpan(0, 0, 1, 14, 365)
      interval2 = TimeSpan.FromTicks(2143756)      
      Console.WriteLine("{0:c} + {1:c} = {2:c}", interval1, 
                        interval2, interval1 + interval2)
   End Sub
End Module
' The example displays the following output:
'       07:45:16 - 18:12:38 = -10:27:22
'       07:45:16 + 18:12:38 = 1.01:57:54
'       00:01:14.3650000 + 00:00:00.2143756 = 00:01:14.5793756

回到表格Back to table

一般短 ("g") 格式規範The General Short ("g") Format Specifier

"g" TimeSpan 格式規範會以壓縮形式傳回 TimeSpan 值的字串表示,而且只包括必要的項目。The "g" TimeSpan format specifier returns the string representation of a TimeSpan value in a compact form by including only the elements that are necessary. 其形式如下:It has the following form:

[-][d:]h:mm:ss[.FFFFFFF][-][d:]h:mm:ss[.FFFFFFF]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 冒號 (:) 是常值符號。The colon (:) is a literal symbol. 下表說明其餘項目。The following table describes the remaining elements.

元素Element 說明Description
- 選擇性的負號,表示負的時間間隔。An optional negative sign, which indicates a negative time interval.
dd 選擇性的天數,沒有前置的零。The optional number of days, with no leading zeros.
hh 小時數,範圍從 "0" 到 "23",且沒有前置的零。The number of hours, which ranges from "0" to "23", with no leading zeros.
mmmm 分鐘數,範圍從 "00" 到 "59"。The number of minutes, which ranges from "00" to "59"..
ssss 秒數,範圍從 "00" 到 "59"。The number of seconds, which ranges from "00" to "59"..
.. 小數的秒數分隔符號。The fractional seconds separator. 它相當於指定之文化特性的 NumberDecimalSeparator 屬性,且不會覆寫使用者。It is equivalent to the specified culture's NumberDecimalSeparator property without user overrides.
FFFFFFFFFFFFFF 小數的秒數。The fractional seconds. 盡可能顯示最少的數字。As few digits as possible are displayed.

和 "G" 格式規範一樣,"g" 格式規範已經過當地語系化。Like the "G" format specifier, the "g" format specifier is localized. 它的小數秒數分隔符號取決於目前的文化特性或指定之文化特性的 NumberDecimalSeparator 屬性。Its fractional seconds separator is based on either the current culture or a specified culture's NumberDecimalSeparator property.

下列範例會呈現兩個 TimeSpan 物件,用以執行算術運算,並顯示結果。The following example instantiates two TimeSpan objects, uses them to perform arithmetic operations, and displays the result. 在每個案例中,都會使用複合格式化,以 "g" 格式規範顯示 TimeSpan 值。In each case, it uses composite formatting to display the TimeSpan value by using the "g" format specifier. 此外,它會使用目前的系統文化特性 (在此案例中是英文-美國或 en-US) 和法文-法國 (fr-FR) 文化特性的格式化慣例,來格式化 TimeSpan 值。In addition, it formats the TimeSpan value by using the formatting conventions of the current system culture (which, in this case, is English - United States or en-US) and the French - France (fr-FR) culture.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      TimeSpan interval1, interval2;
      interval1 = new TimeSpan(7, 45, 16);
      interval2 = new TimeSpan(18, 12, 38);
      
      Console.WriteLine("{0:g} - {1:g} = {2:g}", interval1, 
                        interval2, interval1 - interval2);
      Console.WriteLine(String.Format(new CultureInfo("fr-FR"), 
                        "{0:g} + {1:g} = {2:g}", interval1, 
                        interval2, interval1 + interval2));
      
      interval1 = new TimeSpan(0, 0, 1, 14, 36);
      interval2 = TimeSpan.FromTicks(2143756);      
      Console.WriteLine("{0:g} + {1:g} = {2:g}", interval1, 
                        interval2, interval1 + interval2);
   }
}
// The example displays the following output:
//       7:45:16 - 18:12:38 = -10:27:22
//       7:45:16 + 18:12:38 = 1:1:57:54
//       0:01:14.036 + 0:00:00.2143756 = 0:01:14.2503756
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim interval1, interval2 As TimeSpan
      interval1 = New TimeSpan(7, 45, 16)
      interval2 = New TimeSpan(18, 12, 38)
      
      Console.WriteLine("{0:g} - {1:g} = {2:g}", interval1, 
                        interval2, interval1 - interval2)
      Console.WriteLine(String.Format(New CultureInfo("fr-FR"), 
                        "{0:g} + {1:g} = {2:g}", interval1, 
                        interval2, interval1 + interval2))
      
      interval1 = New TimeSpan(0, 0, 1, 14, 36)
      interval2 = TimeSpan.FromTicks(2143756)      
      Console.WriteLine("{0:g} + {1:g} = {2:g}", interval1, 
                        interval2, interval1 + interval2)
   End Sub
End Module
' The example displays the following output:
'       7:45:16 - 18:12:38 = -10:27:22
'       7:45:16 + 18:12:38 = 1:1:57:54
'       0:01:14.036 + 0:00:00.2143756 = 0:01:14.2503756

回到表格Back to table

一般長 ("G") 格式規範The General Long ("G") Format Specifier

"G" TimeSpan 格式規範會以長形式傳回 TimeSpan 值的字串表示,而此形式一律會同時包含日數和小數秒數。The "G" TimeSpan format specifier returns the string representation of a TimeSpan value in a long form that always includes both days and fractional seconds. 從 "G" 標準格式規範產生的字串具有下列形式:The string that results from the "G" standard format specifier has the following form:

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

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 冒號 (:) 是常值符號。The colon (:) is a literal symbol. 下表說明其餘項目。The following table describes the remaining elements.

元素Element 說明Description
- 選擇性的負號,表示負的時間間隔。An optional negative sign, which indicates a negative time interval.
dd 天數,沒有前置的零。The number of days, with no leading zeros.
hhhh 小時數,範圍從 "00" 到 "23"。The number of hours, which ranges from "00" to "23".
mmmm 分鐘數,範圍從 "00" 到 "59"。The number of minutes, which ranges from "00" to "59".
ssss 秒數,範圍從 "00" 到 "59"。The number of seconds, which ranges from "00" to "59".
.. 小數的秒數分隔符號。The fractional seconds separator. 它相當於指定之文化特性的 NumberDecimalSeparator 屬性,且不會覆寫使用者。It is equivalent to the specified culture's NumberDecimalSeparator property without user overrides.
ffffffffffffff 小數的秒數。The fractional seconds.

和 "G" 格式規範一樣,"g" 格式規範已經過當地語系化。Like the "G" format specifier, the "g" format specifier is localized. 它的小數秒數分隔符號取決於目前的文化特性或指定之文化特性的 NumberDecimalSeparator 屬性。Its fractional seconds separator is based on either the current culture or a specified culture's NumberDecimalSeparator property.

下列範例會呈現兩個 TimeSpan 物件,用以執行算術運算,並顯示結果。The following example instantiates two TimeSpan objects, uses them to perform arithmetic operations, and displays the result. 在每個案例中,都會使用複合格式化,以 "G" 格式規範顯示 TimeSpan 值。In each case, it uses composite formatting to display the TimeSpan value by using the "G" format specifier. 此外,它會使用目前的系統文化特性 (在此案例中是英文-美國或 en-US) 和法文-法國 (fr-FR) 文化特性的格式化慣例,來格式化 TimeSpan 值。In addition, it formats the TimeSpan value by using the formatting conventions of the current system culture (which, in this case, is English - United States or en-US) and the French - France (fr-FR) culture.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      TimeSpan interval1, interval2;
      interval1 = new TimeSpan(7, 45, 16);
      interval2 = new TimeSpan(18, 12, 38);
      
      Console.WriteLine("{0:G} - {1:G} = {2:G}", interval1, 
                        interval2, interval1 - interval2);
      Console.WriteLine(String.Format(new CultureInfo("fr-FR"), 
                        "{0:G} + {1:G} = {2:G}", interval1, 
                        interval2, interval1 + interval2));
      
      interval1 = new TimeSpan(0, 0, 1, 14, 36);
      interval2 = TimeSpan.FromTicks(2143756);      
      Console.WriteLine("{0:G} + {1:G} = {2:G}", interval1, 
                        interval2, interval1 + interval2);
   }
}
// The example displays the following output:
//       0:07:45:16.0000000 - 0:18:12:38.0000000 = -0:10:27:22.0000000
//       0:07:45:16,0000000 + 0:18:12:38,0000000 = 1:01:57:54,0000000
//       0:00:01:14.0360000 + 0:00:00:00.2143756 = 0:00:01:14.2503756
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim interval1, interval2 As TimeSpan
      interval1 = New TimeSpan(7, 45, 16)
      interval2 = New TimeSpan(18, 12, 38)
      
      Console.WriteLine("{0:G} - {1:G} = {2:G}", interval1, 
                        interval2, interval1 - interval2)
      Console.WriteLine(String.Format(New CultureInfo("fr-FR"), 
                        "{0:G} + {1:G} = {2:G}", interval1, 
                        interval2, interval1 + interval2))
      
      interval1 = New TimeSpan(0, 0, 1, 14, 36)
      interval2 = TimeSpan.FromTicks(2143756)      
      Console.WriteLine("{0:G} + {1:G} = {2:G}", interval1, 
                        interval2, interval1 + interval2)
   End Sub
End Module
' The example displays the following output:
'       0:07:45:16.0000000 - 0:18:12:38.0000000 = -0:10:27:22.0000000
'       0:07:45:16,0000000 + 0:18:12:38,0000000 = 1:01:57:54,0000000
'       0:00:01:14.0360000 + 0:00:00:00.2143756 = 0:00:01:14.2503756

回到表格Back to table

另請參閱See also