TimeSpan 格式字串定義格式化作業所產生的 TimeSpan 值的字串表示。 自訂格式字串是由一或多個自定義 TimeSpan 格式規範以及任意數目的常值字元所組成。 任何不是標準 TimeSpan 格式字串 的字串串, 會解譯為自定義 TimeSpan 格式字串。
這很重要
自訂 TimeSpan 格式規範不包含佔位元隔符符號,例如分隔天數與小時、小時與分鐘或秒與小數秒。 相反地,這些符號必須包含在自定義格式字串中做為字串常值。 例如,"dd\.hh\:mm" 將句點定義為天數與小時之間的分隔符,以及冒號(:)為小時與分鐘之間的分隔符。
自訂 TimeSpan 格式規範也不包含符號符號,可讓您區分負數和正時間間隔。 若要包含符號符號,您必須使用條件式邏輯來建構格式字串。 [其他字元] 區段包含範例。
TimeSpan 值的字串表示是由呼叫 TimeSpan.ToString 方法的多載,以及支援複合格式的方法所產生,例如 String.Format。 如需詳細資訊,請參閱 格式化類型 和 複合格式。 下列範例說明在格式化作業中使用自定義格式字串。
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: {duration:%d} day(s)");
Console.WriteLine($"Time of Travel: {duration:dd\\.hh\\:mm\\:ss} days");
}
}
// 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 和 TimeSpan.ParseExact 方法也會使用自訂 TimeSpan.TryParseExact 格式字串,以定義剖析作業所需的輸入字串格式。 (剖析會將值的字串表示轉換為該值。下列範例說明在剖析作業中使用標準格式字串。
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($"{value} --> {interval.ToString("c")}");
else
Console.WriteLine($"Unable to parse '{value}'");
value = "16:32.05";
if (TimeSpan.TryParseExact(value, @"mm\:ss\.ff", null, out interval))
Console.WriteLine($"{value} --> {interval.ToString("c")}");
else
Console.WriteLine($"Unable to parse '{value}'");
value= "12.035";
if (TimeSpan.TryParseExact(value, "ss\\.fff", null, out interval))
Console.WriteLine($"{value} --> {interval.ToString("c")}");
else
Console.WriteLine($"Unable to parse '{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
| 格式規範 | 說明 | 範例 |
|---|---|---|
| “d”, “%d” | 時間間隔中的整天數。 詳細資訊: “d” 自定義格式規範。 |
new TimeSpan(6, 14, 32, 17, 685):%d -> “6”d\.hh\:mm --> “6.14:32” |
| “dd”-“dddddddd” | 時間間隔中的整天數,視需要填補前置零。 詳細資訊:「dd」-“ddddd” 自訂格式規範。 |
new TimeSpan(6, 14, 32, 17, 685):ddd --> “006”dd\.hh\:mm --> “06.14:32” |
| “h”, “%h” | 時間間隔中未計入天數的整小時數。 單一位數時數沒有前置零。 詳細資訊: “h” 自定義格式規範。 |
new TimeSpan(6, 14, 32, 17, 685):%h -> “14”hh\:mm --> “14:32” |
| “hh” | 時間間隔中未計入天數的整小時數。 單一位數時數有前置零。 詳細資訊: “hh” 自定義格式規範。 |
new TimeSpan(6, 14, 32, 17, 685):hh -> “14”new TimeSpan(6, 8, 32, 17, 685):hh --> 08 |
| “m”, “%m” | 時間間隔中未包含在小時或天數內的整分鐘數。 單位數分鐘沒有前置零。 詳細資訊: “m” 自定義格式規範。 |
new TimeSpan(6, 14, 8, 17, 685):%m --> “8”h\:m -> “14:8” |
| “mm” | 時間間隔中未包含在小時或天數內的整分鐘數。 單一位數分鐘具有前置零。 詳細資訊: “mm” 自定義格式規範。 |
new TimeSpan(6, 14, 8, 17, 685):mm --> “08”new TimeSpan(6, 8, 5, 17, 685):d\.hh\:mm\:ss --> 6.08:05:17 |
| “s”, “%s” | 時間間隔中未包含在小時、天數或分鐘中的整秒數。 單一位數秒沒有前置零。 詳細資訊: “s” 自定義格式規範。 |
TimeSpan.FromSeconds(12.965):%s --> 12s\.fff --> 12.965 |
| “ss” | 時間間隔中未包含在小時、天數或分鐘中的整秒數。 單一位數秒有前置零。 詳細資訊: “ss” 自定義格式規範。 |
TimeSpan.FromSeconds(6.965):ss --> 06ss\.fff --> 06.965 |
| “f”, “%f” | 時間間隔中的十分之一秒。 詳細資訊: “f” 自定義格式規範。 |
TimeSpan.FromSeconds(6.895):f --> 8ss\.f --> 06.8 |
| “ff” | 時間間隔中的百分之一秒。 詳細資訊: “ff” 自定義格式規範。 |
TimeSpan.FromSeconds(6.895):ff --> 89ss\.ff --> 06.89 |
| “fff” | 時間間隔中的毫秒。 詳細資訊: “fff” 自定義格式規範。 |
TimeSpan.FromSeconds(6.895):fff --> 895ss\.fff --> 06.895 |
| “ffff” | 時間間隔中的十萬分之一秒。 詳細資訊: “ffff” 自定義格式規範。 |
TimeSpan.Parse("0:0:6.8954321"):ffff --> 8954ss\.ffff --> 06.8954 |
| “fffff” | 時間間隔中的秒數十萬分之一。 詳細資訊: “fffff” 自定義格式規範。 |
TimeSpan.Parse("0:0:6.8954321"):fffff --> 89543ss\.fffff --> 06.89543 |
| “ffffff” | 時間間隔中的每秒百萬分之一。 詳細資訊: “ffffff” 自定義格式規範。 |
TimeSpan.Parse("0:0:6.8954321"):ffffff --> 895432ss\.ffffff --> 06.895432 |
| “fffffff” | 時間間隔中秒的千萬分之一(或小數刻度)。 詳細資訊: “fffffff” 自定義格式規範。 |
TimeSpan.Parse("0:0:6.8954321"):fffffff --> 8954321ss\.fffffff --> 06.8954321 |
| “F”, “%F” | 時間間隔中的十分之一秒。 如果數位為零,則不會顯示任何內容。 詳細資訊: “F” 自定義格式規範。 |
TimeSpan.Parse("00:00:06.32"):%F: 3TimeSpan.Parse("0:0:3.091"):ss\.F: 03。 |
| “FF” | 時間間隔中的百分之一秒。 不包含任何小數尾零或兩個零位數。 詳細資訊: “FF” 自定義格式規範。 |
TimeSpan.Parse("00:00:06.329"):FF: 32TimeSpan.Parse("0:0:3.101"):ss\.FF: 03.1 |
| “FFF” | 時間間隔中的毫秒。 不包含任何小數尾尾零。 其他資訊: |
TimeSpan.Parse("00:00:06.3291"):FFF: 329TimeSpan.Parse("0:0:3.1009"):ss\.FFF: 03.1 |
| “FFFF” | 時間間隔中的十萬分之一秒。 不包含任何小數尾尾零。 詳細資訊: “FFFF” 自定義格式規範。 |
TimeSpan.Parse("00:00:06.32917"):FFFFF: 3291TimeSpan.Parse("0:0:3.10009"):ss\.FFFF: 03.1 |
| “FFFFF” | 時間間隔中的秒數十萬分之一。 不包含任何小數尾尾零。 詳細資訊: “FFFFF” 自定義格式規範。 |
TimeSpan.Parse("00:00:06.329179"):FFFFF: 32917TimeSpan.Parse("0:0:3.100009"):ss\.FFFFF: 03.1 |
| “FFFFFF” | 時間間隔中的每秒百萬分之一。 不會顯示任何小數尾尾零。 詳細資訊: “FFFFFF” 自定義格式規範。 |
TimeSpan.Parse("00:00:06.3291791"):FFFFFF:329179TimeSpan.Parse("0:0:3.1000009"):ss\.FFFFFF: 03.1 |
| “FFFFFFF” | 時間間隔中的1000萬秒。 不會顯示任何小數尾零或七個零。 詳細資訊: “FFFFFFF” 自定義格式規範。 |
TimeSpan.Parse("00:00:06.3291791"):FFFFFF: 3291791TimeSpan.Parse("0:0:3.1900000"):ss\.FFFFFF: 03.19 |
| '字串' | 常值字串分隔符號。 詳細資訊:其他字元。 |
new TimeSpan(14, 32, 17):hh':'mm':'ss --> “14:32:17” |
| \ | 逸出字元。 詳細資訊:其他字元。 |
new TimeSpan(14, 32, 17):hh\:mm\:ss --> “14:32:17” |
| 任意字元 | 任何其他未逸出字元會解譯為自定義格式規範。 詳細資訊:其他字元。 |
new TimeSpan(14, 32, 17):hh\:mm\:ss --> “14:32:17” |
"d" 自訂格式規範
“d” 自定義格式規範會輸出 TimeSpan.Days 屬性的值,代表時間間隔中的整天數。 它會輸出 TimeSpan 值中的完整天數,即使值有一個以上的數位也一樣。 如果 TimeSpan.Days 屬性值為零,規範會輸出 “0”。
如果單獨使用 「d」 自訂格式規範,請指定 「%d」,使其不會誤譯為標準格式字串。 下列範例提供一個實例。
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」 自訂格式規範。
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
“dd”-“ddddd” 自定義格式規範
“dd”、“d”、“d”、“d”、“d”、“d” 和 “d” 自定義格式規範會輸出 TimeSpan.Days 屬性的值,代表時間間隔中的整天數。
輸出字串包含格式規範中 「d」 字元數目所指定的最小位數,而且會視需要以前置零填補。 如果天數中的數字超過格式規範中的 「d」 字元數,結果字串中會輸出完整的天數。
下列範例會使用這些格式規範來顯示兩個 TimeSpan 值的字串表示。 第一個時間間隔的 days 元件值為零;秒的天元件值為 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($"{fmt} --> {ts1.ToString(fmt)}");
Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
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
"h" 自訂格式規範
“h” 自定義格式規範會輸出 TimeSpan.Hours 屬性的值,代表時間間隔中未計入其日元件一部分的整數小時數。 如果 TimeSpan.Hours 屬性的值是 0 到 9,則會傳回一位數位符串值,如果 TimeSpan.Hours 屬性值的範圍從 10 到 23,則會傳回兩位數位符串值。
如果單獨使用 「h」 自訂格式規範,請指定 「%h」,使其不會誤譯為標準格式字串。 下列範例提供一個實例。
TimeSpan ts = new TimeSpan(3, 42, 0);
Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
// 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
一般而言,在剖析作業中,只包含單一數字的輸入字串會解譯為天數。 您可以改用 「%h」 自訂格式規範,將數值字串解譯為時數。 下列範例提供一個實例。
string value = "8";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "%h", null, out interval))
Console.WriteLine(interval.ToString("c"));
else
Console.WriteLine($"Unable to convert '{value}' to a time interval");
// 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」 自訂格式規範。
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
"hh" 自訂格式規範
“hh” 自定義格式規範會輸出 TimeSpan.Hours 屬性的值,代表時間間隔中不算為其日元件一部分的整數小時數。 對於 0 到 9 的值,輸出字串包含前置零。
一般而言,在剖析作業中,只包含單一數字的輸入字串會解譯為天數。 您可以改用 「hh」 自訂格式規範,將數值字串解譯為時數。 下列範例提供一個實例。
string value = "08";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "hh", null, out interval))
Console.WriteLine(interval.ToString("c"));
else
Console.WriteLine($"Unable to convert '{value}' to a time interval");
// 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」 自訂格式規範。
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
"m" 自訂格式規範
“m” 自定義格式規範會輸出 TimeSpan.Minutes 屬性的值,代表時間間隔中未計入其日元件一部分的整分鐘數。 如果 TimeSpan.Minutes 屬性的值是 0 到 9,則會傳回一位數位符串值,如果 TimeSpan.Minutes 屬性值的範圍從 10 到 59,則會傳回兩位數的字串值。
如果單獨使用 「m」 自訂格式規範,請指定 「%m」,使其不會誤譯為標準格式字串。 下列範例提供一個實例。
TimeSpan ts = new TimeSpan(3, 42, 0);
Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
// 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
一般而言,在剖析作業中,只包含單一數字的輸入字串會解譯為天數。 您可以改用 「%m」 自訂格式規範,將數值字串解譯為分鐘數。 下列範例提供一個實例。
string value = "3";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "%m", null, out interval))
Console.WriteLine(interval.ToString("c"));
else
Console.WriteLine($"Unable to convert '{value}' to a time interval");
// 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」 自訂格式規範。
TimeSpan ts1 = new TimeSpan(0, 6, 32);
Console.WriteLine($"{ts1:m\\:ss} minutes");
TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
// 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
"mm" 自訂格式規範
“mm” 自定義格式規範會輸出 TimeSpan.Minutes 屬性的值,代表時間間隔中未包含在其小時或天數元件中的整分鐘數。 對於 0 到 9 的值,輸出字串包含前置零。
一般而言,在剖析作業中,只包含單一數字的輸入字串會解譯為天數。 您可以改用 「mm」 自訂格式規範,將數值字串解譯為分鐘數。 下列範例提供一個實例。
string value = "07";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "mm", null, out interval))
Console.WriteLine(interval.ToString("c"));
else
Console.WriteLine($"Unable to convert '{value}' to a time interval");
// 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」 自訂格式規範。
TimeSpan departTime = new TimeSpan(11, 12, 00);
TimeSpan arriveTime = new TimeSpan(16, 28, 00);
Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
// 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
"s" 自訂格式規範
“s” 自定義格式規範會輸出 TimeSpan.Seconds 屬性的值,代表時間間隔中未包含在其小時、天數或分鐘元件中的整數秒數。 如果 TimeSpan.Seconds 屬性的值是 0 到 9,則會傳回一位數位符串值,如果 TimeSpan.Seconds 屬性值的範圍從 10 到 59,則會傳回兩位數的字串值。
如果單獨使用 「s」 自訂格式規範,請指定 「%s」,使其不會誤譯為標準格式字串。 下列範例提供一個實例。
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
一般而言,在剖析作業中,只包含單一數字的輸入字串會解譯為天數。 您可以改用 「%s」 自訂格式規範,將數值字串解譯為秒數。 下列範例提供一個實例。
string value = "9";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "%s", null, out interval))
Console.WriteLine(interval.ToString("c"));
else
Console.WriteLine($"Unable to convert '{value}' to a time interval");
// 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」 自訂格式規範。
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
"ss" 自訂格式規範
“ss” 自定義格式規範會輸出 TimeSpan.Seconds 屬性的值,代表時間間隔中未包含在其小時、天數或分鐘元件中的整數秒數。 對於 0 到 9 的值,輸出字串包含前置零。
一般而言,在剖析作業中,只包含單一數字的輸入字串會解譯為天數。 您可以改用 「ss」 自訂格式規範,將數值字串解譯為秒數。 下列範例提供一個實例。
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 '{value}' to a time interval");
}
// 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」 自訂格式規範。
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
"f" 自訂格式規範
“f” 自定義格式規範會輸出時間間隔中的十分之一秒。 在格式化作業中,會截斷任何剩餘的小數位數。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,輸入字元串必須只包含一個小數字數。
如果單獨使用 「f」 自訂格式規範,請指定 「%f」,使其不會誤譯為標準格式字串。
下列範例會使用 「f」 自訂格式規範,在 TimeSpan 值中顯示十分之一秒。 “f” 會先作為唯一的格式規範使用,然後在自定義格式字串中結合 “s” 規範。
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
"ff" 自訂格式規範
“ff” 自定義格式規範會輸出時間間隔中的百分之一秒。 在格式化作業中,會截斷任何剩餘的小數位數。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,輸入字元串必須只包含兩個小數字數。
下列範例會使用 「ff」 自訂格式規範,在 TimeSpan 值中顯示十分之一秒。 “ff” 會先作為唯一的格式規範使用,然後在自定義格式字串中結合 “s” 規範。
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
"fff" 自訂格式規範
“fff” 自定義格式規範 (含三個 “f” 字元) 會輸出時間間隔中的毫秒。 在格式化作業中,會截斷任何剩餘的小數位數。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,輸入字元串必須只包含三個小數字數。
下列範例會使用 「fff」 自訂格式規範,在 TimeSpan 值中顯示毫秒。 “fff” 會先作為唯一的格式規範使用,然後在自定義格式字串中結合 “s” 規範。
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
"ffff" 自訂格式規範
“ffff” 自定義格式規範 (包含四個 “f” 字元)會以時間間隔輸出每秒的十萬分之一。 在格式化作業中,會截斷任何剩餘的小數位數。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,輸入字串必須只包含四個小數字數。
下列範例會使用 「ffff」 自訂格式規範,在 TimeSpan 值中顯示每秒 1 萬分之一。 “ffff” 會先作為唯一的格式規範使用,然後在自定義格式字串中結合 “s” 規範。
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
"fffff" 自訂格式規範
“fffff” 自定義格式規範(包含五個 “f” 字元)會以時間間隔輸出每秒的十萬分之一。 在格式化作業中,會截斷任何剩餘的小數位數。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,輸入字元串必須只包含五個小數字數。
下列範例會使用 「fffff」 自訂格式規範,在 TimeSpan 值中顯示秒數十萬分之一。 “fffff” 會先作為唯一的格式規範使用,然後在自定義格式字元串中結合 “s” 規範。
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
"ffffff" 自訂格式規範
“ffffff” 自定義格式規範(包含六個 “f” 字元)會以時間間隔輸出每秒的百萬分之一。 在格式化作業中,會截斷任何剩餘的小數位數。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,輸入字元串必須只包含六個小數字數。
下列範例會使用 「ffffff」 自訂格式規範,在 TimeSpan 值中顯示秒數百萬分之一。 它會先作為唯一的格式規範使用,然後在自定義格式字串中結合 “s” 規範。
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
"fffffff" 自訂格式規範
“fffffff” 自定義格式規範 (包含七個 “f” 字元)會以時間間隔輸出秒數(或刻度分數數)的十萬分之一秒(或分數數目)。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,輸入字串必須只包含七個小數字數。
下列範例會使用 「fffffff」 自訂格式規範,在 TimeSpan 值中顯示刻度的小數。 它會先作為唯一的格式規範使用,然後在自定義格式字串中結合 “s” 規範。
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
"F" 自訂格式規範
“F” 自定義格式規範會輸出時間間隔中的十分之一秒。 在格式化作業中,會截斷任何剩餘的小數位數。 如果時間間隔十分之一秒的值為零,則結果字串中不會包含該值。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,第十秒位數的存在是選擇性的。
如果單獨使用 「F」 自訂格式規範,請指定 「%F」,使其不會誤譯為標準格式字串。
下列範例會使用 「F」 自訂格式規範,在 TimeSpan 值中顯示十分之一秒。 它也會在剖析作業中使用這個自定義格式規範。
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.669");
Console.WriteLine($"{ts1} ('%F') --> {ts1:%F}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.091");
Console.WriteLine($"{ts2} ('ss\\.F') --> {ts2:ss\\.F}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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'.
"FF" 自訂格式規範
“FF” 自定義格式規範會輸出時間間隔中的百分之一秒。 在格式化作業中,會截斷任何剩餘的小數位數。 如果有任何尾端小數零,則結果字串中不會包含它們。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,十分之一和百分之一秒數位的存在是選擇性的。
下列範例使用 「FF」 自訂格式規範,在 TimeSpan 值中顯示十分之一秒。 它也會在剖析作業中使用這個自定義格式規範。
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.697");
Console.WriteLine($"{ts1} ('FF') --> {ts1:FF}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.809");
Console.WriteLine($"{ts2} ('ss\\.FF') --> {ts2:ss\\.FF}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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'.
"FFF" 自訂格式規範
“FFF” 自定義格式規範 (具有三個 “F” 字元)會輸出時間間隔中的毫秒。 在格式化作業中,會截斷任何剩餘的小數位數。 如果有任何尾端小數零,則結果字串中不會包含它們。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,十分之一、百分之一和千分之一秒數位的存在是選擇性的。
下列範例使用 「FFF」 自訂格式規範,在 TimeSpan 值中顯示每秒千分之一秒。 它也會在剖析作業中使用這個自定義格式規範。
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974");
Console.WriteLine($"{ts1} ('FFF') --> {ts1:FFF}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.8009");
Console.WriteLine($"{ts2} ('ss\\.FFF') --> {ts2:ss\\.FFF}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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'.
"FFFF" 自訂格式規範
“FFFF” 自定義格式規範(包含四個 “F” 字元)會以時間間隔輸出每秒十萬分之一。 在格式化作業中,會截斷任何剩餘的小數位數。 如果有任何尾端小數零,則結果字串中不會包含它們。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,十分之一、十、十、千分之一和十萬分之一秒的數位是選擇性的。
下列範例會使用 「FFFF」 自訂格式規範,在 TimeSpan 值中顯示每秒 1 萬分之一。 它也會在剖析作業中使用 「FFFF」 自訂格式規範。
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.69749");
Console.WriteLine($"{ts1} ('FFFF') --> {ts1:FFFF}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.80009");
Console.WriteLine($"{ts2} ('ss\\.FFFF') --> {ts2:ss\\.FFFF}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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'.
"FFFFF" 自訂格式規範
“FFFFF” 自定義格式規範 (包含五個 “F” 字元)會以時間間隔輸出每秒的十萬分之一。 在格式化作業中,會截斷任何剩餘的小數位數。 如果有任何尾端小數零,則結果字串中不會包含它們。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,第十個、十分之一、千分之一、千分之一和十萬分之一秒數位的存在是選擇性的。
下列範例會使用 「FFFFF」 自訂格式規範,在 TimeSpan 值中顯示秒數十萬分之一。 它也會在剖析作業中使用 「FFFFF」 自訂格式規範。
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.697497");
Console.WriteLine($"{ts1} ('FFFFF') --> {ts1:FFFFF}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.800009");
Console.WriteLine($"{ts2} ('ss\\.FFFFF') --> {ts2:ss\\.FFFFF}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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'.
"FFFFFF" 自訂格式規範
“FFFFFF” 自定義格式規範(包含六個 “F” 字元)會以時間間隔輸出每秒的百萬分之一。 在格式化作業中,會截斷任何剩餘的小數位數。 如果有任何尾端小數零,則結果字串中不會包含它們。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,第十個、千分之一、千分之一、十萬分之一和百萬分之一秒的數位是選擇性的。
下列範例會使用 「FFFFFF」 自訂格式規範,在 TimeSpan 值中顯示秒數百萬分之一。 它也會在剖析作業中使用這個自定義格式規範。
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
Console.WriteLine($"{ts1} ('FFFFFF') --> {ts1:FFFFFF}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.8000009");
Console.WriteLine($"{ts2} ('ss\\.FFFFFF') --> {ts2:ss\\.FFFFFF}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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'.
"FFFFFFF" 自訂格式規範
“FFFFFFF” 自定義格式規範(包含七個 “F” 字元)會以時間間隔輸出秒數(或刻度分數數)的十萬分之一。 如果有任何尾端小數零,則結果字串中不會包含它們。 在呼叫 TimeSpan.ParseExact 或 TimeSpan.TryParseExact 方法的剖析作業中,輸入字元串中存在七個小數位數是選擇性的。
下列範例會使用 「FFFFFFF」 自訂格式規範,在 TimeSpan 值中顯示秒的小數部分。 它也會在剖析作業中使用這個自定義格式規範。
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
Console.WriteLine($"{ts1} ('FFFFFFF') --> {ts1:FFFFFFF}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.9500000");
Console.WriteLine($"{ts2} ('ss\\.FFFFFFF') --> {ts2:ss\\.FFFFFFF}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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
其他字元
格式字串中任何其他未逸出的字元,包括空格符,會解譯為自定義格式規範。 在大部分情況下,任何其他未逸出字元的存在會導致 FormatException。
有兩種方式可將常值字元包含在格式字串中:
以單引弧括住它(常值字串分隔符)。
在它前面加上反斜杠 (“\”),它會解譯為逸出字元。 這表示,在 C# 中,格式字串必須是 @-quoted,或常值字元前面必須加上額外的反斜杠。
在某些情況下,您可能必須使用條件式邏輯,在格式字串中包含逸出常值。 下列範例會使用條件式邏輯來包含負時間間隔的符號符號。
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: {result.ToString(fmt)}"); } } // The example displays output like the following: // -5582.12:21 // Interval: -5582.12:21Module 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 不會定義時間間隔中分隔符的文法。 這表示日和小時、小時、分鐘、分、秒和秒和秒之間的分隔符,都必須在格式字串中視為字元常值。
下列範例會使用逸出字元和單引號來定義自定義格式字元串,其中包含輸出字串中的 “minutes” 一詞。
TimeSpan interval = new TimeSpan(0, 32, 45);
// Escape literal characters in a format string.
string fmt = @"mm\:ss\ \m\i\n\u\t\e\s";
Console.WriteLine(interval.ToString(fmt));
// Delimit literal characters in a format string with the ' symbol.
fmt = "mm':'ss' minutes'";
Console.WriteLine(interval.ToString(fmt));
// The example displays the following output:
// 32:45 minutes
// 32:45 minutes
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