カスタム TimeSpan 書式指定文字列

TimeSpan 書式指定文字列は、書式設定操作によって生成される TimeSpan 値の文字列形式を定義します。 カスタム書式指定文字列は、1 つ以上のカスタム 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: {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 書式指定文字列は、解析操作に必要な入力文字列の書式を定義するために 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("{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

カスタム日時書式指定子を次の表に示します。

書式指定子 説明
"d" または "%d" 時間間隔の日数。

詳細情報:"d" カスタム書式指定子
new TimeSpan(6, 14, 32, 17, 685):

%d --> "6"

d\.hh\:mm --> "6.14:32"
"dd" ~ "dddddddd" 必要に応じて先行ゼロで埋められた、時間間隔の日数。

詳細情報:"dd" - "dddddddd" カスタム書式指定子
new TimeSpan(6, 14, 32, 17, 685):

ddd --> "006"

dd\.hh\:mm --> "06.14:32"
"h" または "%h" 日数の一部としてカウントされない、時間間隔の時間数。 1 桁の時間に先行ゼロは付きません。

詳細情報:"h" カスタム書式指定子
new TimeSpan(6, 14, 32, 17, 685):

%h --> "14"

hh\:mm --> "14:32"
"hh" 日数の一部としてカウントされない、時間間隔の時間数。 1 桁の時間には、先頭に 0 が付けられます。

詳細情報:"hh" カスタム書式指定子
new TimeSpan(6, 14, 32, 17, 685):

hh --> "14"

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

hh --> 08
"m" または "%m" 時間数および日数のいずれの一部としても含まれない、時間間隔の分数。 1 桁の分に先行ゼロは付きません。

詳細情報:"m" カスタム書式指定子
new TimeSpan(6, 14, 8, 17, 685):

%m --> "8"

h\:m --> "14:8"
"mm" 時間数および日数のいずれの一部としても含まれない、時間間隔の分数。 1 桁の分には、先頭に 0 が付きます。

詳細情報:"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" 時間数、日数、および分数のいずれの一部としても含まれない、時間間隔の秒数。 1 桁の秒に先行ゼロは付きません。

詳細情報:"s" カスタム書式指定子
TimeSpan.FromSeconds(12.965):

%s --> 12

s\.fff --> 12.965
"ss" 時間数、日数、および分数のいずれの一部としても含まれない、時間間隔の秒数。 1 桁の秒には、先頭に 0 が付きます。

詳細情報:"ss" カスタム書式指定子
TimeSpan.FromSeconds(6.965):

ss --> 06

ss\.fff --> 06.965
"f" または "%f" 時間間隔の秒部分の 1/10。

詳細情報:"F" カスタム書式指定子
TimeSpan.FromSeconds(6.895):

f --> 8

ss\.f --> 06.8
"ff" 時間間隔の秒部分の 1/100。

詳細情報:"ff" カスタム書式指定子
TimeSpan.FromSeconds(6.895):

ff --> 89

ss\.ff --> 06.89
"fff" 時間間隔の秒部分の 1/1000。

詳細情報:"fff" カスタム書式指定子
TimeSpan.FromSeconds(6.895):

fff --> 895

ss\.fff --> 06.895
"ffff" 時間間隔の秒部分の 1/10000。

詳細情報:"ffff" カスタム書式指定子
TimeSpan.Parse("0:0:6.8954321"):

ffff --> 8954

ss\.ffff --> 06.8954
"fffff" 時間間隔の秒部分の 1/100000。

詳細情報:"fffff" カスタム書式指定子
TimeSpan.Parse("0:0:6.8954321"):

fffff --> 89543

ss\.fffff --> 06.89543
"ffffff" 時間間隔の秒部分の 1/1000000。

詳細情報:"ffffff" カスタム書式指定子
TimeSpan.Parse("0:0:6.8954321"):

ffffff --> 895432

ss\.ffffff --> 06.895432
"fffffff" 時間間隔の秒部分の 1/10000000 (またはタイマー刻みの小数部)。

詳細情報:"fffffff" カスタム書式指定子
TimeSpan.Parse("0:0:6.8954321"):

fffffff --> 8954321

ss\.fffffff --> 06.8954321
"F" または "%F" 時間間隔の秒部分の 1/10。 その桁がゼロの場合には、何も表示されません。

詳細情報:"F" カスタム書式指定子
TimeSpan.Parse("00:00:06.32"):

%F:3

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

ss\.F:03。
"FF" 時間間隔の秒部分の 1/100。 小数の後続のゼロは表示されません。また、2 桁のゼロも含まれません。

詳細情報:"FF" カスタム書式指定子
TimeSpan.Parse("00:00:06.329"):

FF:32

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

ss\.FF:03.1
"FFF" 時間間隔の秒部分の 1/1000。 小数の後続のゼロは含まれません。

詳細情報:
TimeSpan.Parse("00:00:06.3291"):

FFF:329

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

ss\.FFF:03.1
"FFFF" 時間間隔の秒部分の 1/10000。 小数の後続のゼロは含まれません。

詳細情報:"FFFF" カスタム書式指定子
TimeSpan.Parse("00:00:06.32917"):

FFFFF:3291

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

ss\.FFFF:03.1
"FFFFF" 時間間隔の秒部分の 1/100000。 小数の後続のゼロは含まれません。

詳細情報:"FFFFF" カスタム書式指定子
TimeSpan.Parse("00:00:06.329179"):

FFFFF:32917

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

ss\.FFFFF:03.1
"FFFFFF" 時間間隔の秒部分の 1/1000000。 小数の後続のゼロは表示されません。

詳細情報:"FFFFFF" カスタム書式指定子
TimeSpan.Parse("00:00:06.3291791"):

FFFFFF:329179

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

ss\.FFFFFF:03.1
"FFFFFFF" 時間間隔の秒部分の 1/10000000。 小数の後続のゼロは表示されません。また、7 桁のゼロも表示されません。

詳細情報:"FFFFFFF" カスタム書式指定子
TimeSpan.Parse("00:00:06.3291791"):

FFFFFF:3291791

TimeSpan.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 プロパティの値を出力します。 値が 1 桁を超える場合でも、TimeSpan 値の完全な日数が出力されます。 TimeSpan.Days プロパティの値が 0 の場合、指定子は "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" ~ "dddddddd" カスタム書式指定子

"dd"、"ddd"、"dddd"、"ddddd"、"dddddd"、"ddddddd"、および "dddddddd" カスタム書式指定子は、時間間隔の日数を表す TimeSpan.Days プロパティの値を出力します。

出力文字列には、書式指定子の "d" 文字の数で指定される桁数以上が含まれ、必要に応じて、先行ゼロで埋められます。 日数の桁数が書式指定子の "d" 文字の数を超える場合は、結果文字列に完全な日数が出力されます。

次の例では、これらの書式指定子を使用して、2 つの TimeSpan 値の文字列形式を表示します。 最初の時間間隔の日の部分の値は 0 です。2 番目の時間間隔の日の部分の値は 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      

表のトップへ

"h" カスタム書式指定子

"h" カスタム書式指定子は、日の部分の一部としてカウントされない、時間間隔の時間数を表す TimeSpan.Hours プロパティの値を出力します。 TimeSpan.Hours プロパティの値が 0 ~ 9 の場合は 1 桁の文字列値を返し、TimeSpan.Hours プロパティの値が 10 ~ 23 の場合は 2 桁の文字列値を返します。

"h" カスタム書式指定子が単独で使用される場合は、誤って標準書式指定文字列として解釈されないように "%h" を指定します。 具体的な例を次に示します。

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

通常、解析操作では、1 つの数値のみを含む入力文字列は日数として解釈されます。 代わりに "%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 '{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" カスタム書式指定子の使用例を次に示します。

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 の値の場合は、出力文字列に先行ゼロが含まれます。

通常、解析操作では、1 つの数値のみを含む入力文字列は日数として解釈されます。 代わりに "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 '{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" カスタム書式指定子の使用例を次に示します。

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 の場合は 1 桁の文字列値を返し、TimeSpan.Minutes プロパティの値が 10 ~ 59 の場合は 2 桁の文字列値を返します。

"m" カスタム書式指定子が単独で使用される場合は、誤って標準書式指定文字列として解釈されないように "%m" を指定します。 具体的な例を次に示します。

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

通常、解析操作では、1 つの数値のみを含む入力文字列は日数として解釈されます。 代わりに "%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 '{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" カスタム書式指定子の使用例を次に示します。

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

表のトップへ

"mm" カスタム書式指定子

"mm" カスタム書式指定子は、時間または日の部分の一部として含まれない、時間間隔の分数を表す TimeSpan.Minutes プロパティの値を出力します。 0 ~ 9 の値の場合は、出力文字列に先行ゼロが含まれます。

通常、解析操作では、1 つの数値のみを含む入力文字列は日数として解釈されます。 代わりに "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 '{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" カスタム書式指定子の使用例を次に示します。

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      

表のトップへ

"s" カスタム書式指定子

"s" カスタム書式指定子は、時間、日、および分の部分のいずれの一部としても含まれない、時間間隔の秒数を表す TimeSpan.Seconds プロパティの値を出力します。 TimeSpan.Seconds プロパティの値が 0 ~ 9 の場合は 1 桁の文字列値を返し、TimeSpan.Seconds プロパティの値が 10 ~ 59 の場合は 2 桁の文字列値を返します。

"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

通常、解析操作では、1 つの数値のみを含む入力文字列は日数として解釈されます。 代わりに "%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 '{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" カスタム書式指定子の使用例を次に示します。

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 の値の場合は、出力文字列に先行ゼロが含まれます。

通常、解析操作では、1 つの数値のみを含む入力文字列は日数として解釈されます。 代わりに "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 '{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" カスタム書式指定子の使用例を次に示します。

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" カスタム書式指定子は、時間間隔の秒部分の 1/10 を出力します。 書式指定操作では、これより下位の小数桁は切り捨てられます。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、入力文字列に 1 桁の小数部が含まれている必要があります。

"f" カスタム書式指定子が単独で使用される場合は、誤って標準書式指定文字列として解釈されないように "%f" を指定します。

次の例では、"f" カスタム書式指定子を使用して、TimeSpan 値の秒部分の 1/10 を表示します。 "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("{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

表のトップへ

"ff" カスタム書式指定子

"ff" カスタム書式指定子は、時間間隔の秒部分の 1/100 を出力します。 書式指定操作では、これより下位の小数桁は切り捨てられます。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、入力文字列に 2 桁の小数部が含まれている必要があります。

次の例では、"ff" カスタム書式指定子を使用して、TimeSpan 値の秒部分の 1/100 を表示します。 "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("{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

表のトップへ

"fff" カスタム書式指定子

"fff" カスタム書式指定子 ("f" 文字が 3 つ) は、時間間隔の秒部分の 1/1000 を出力します。 書式指定操作では、これより下位の小数桁は切り捨てられます。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、入力文字列に 3 桁の小数部が含まれている必要があります。

次の例では、"fff" カスタム書式指定子を使用して、TimeSpan 値の秒部分の 1/1000 を表示します。 "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("{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

表のトップへ

"ffff" カスタム書式指定子

"ffff" カスタム書式指定子 ("f" 文字が 4 つ) は、時間間隔の秒部分の 1/10000 を出力します。 書式指定操作では、これより下位の小数桁は切り捨てられます。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、入力文字列に 4 桁の小数部が含まれている必要があります。

次の例では、"ffff" カスタム書式指定子を使用して、TimeSpan 値の秒部分の 1/10000 を表示します。 "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("{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

表のトップへ

"fffff" カスタム書式指定子

"fffff" カスタム書式指定子 ("f" 文字が 5 つ) は、時間間隔の秒部分の 1/100000 を出力します。 書式指定操作では、これより下位の小数桁は切り捨てられます。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、入力文字列に 5 桁の小数部が含まれている必要があります。

次の例では、"fffff" カスタム書式指定子を使用して、TimeSpan 値の秒部分の 1/100000 を表示します。 "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("{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

表のトップへ

"ffffff" カスタム書式指定子

"ffffff" カスタム書式指定子 ("f" 文字が 6 つ) は、時間間隔の秒部分の 1/1000000 を出力します。 書式指定操作では、これより下位の小数桁は切り捨てられます。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、入力文字列に 6 桁の小数部が含まれている必要があります。

次の例では、"ffffff" カスタム書式指定子を使用して、TimeSpan 値の秒部分の 1/1000000 を表示します。 最初に唯一の書式指定子として使用されてから、カスタム書式指定文字列で "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("{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

表のトップへ

"fffffff" カスタム書式指定子

"fffffff" カスタム書式指定子 ("f" 文字が 7 つ) は、時間間隔の秒部分の 1/10000000 (またはタイマー刻みの小数部) を出力します。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、入力文字列に 7 桁の小数部が含まれている必要があります。

次の例では、"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("{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

表のトップへ

"F" カスタム書式指定子

"F" カスタム書式指定子は、時間間隔の秒部分の 1/10 を出力します。 書式指定操作では、これより下位の小数桁は切り捨てられます。 時間間隔の秒部分の 1/10 が 0 の場合、それは結果文字列に含まれません。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、秒の 1/10 の桁を使用するかどうかはオプションです。

"F" カスタム書式指定子が単独で使用される場合は、誤って標準書式指定文字列として解釈されないように "%F" を指定します。

次の例では、"F" カスタム書式指定子を使用して、TimeSpan 値の秒部分の 1/10 を表示します。 このカスタム書式指定子は解析操作でも使用されます。

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

表のトップへ

"FF" カスタム書式指定子

"FF" カスタム書式指定子は、時間間隔の秒部分の 1/100 を出力します。 書式指定操作では、これより下位の小数桁は切り捨てられます。 後続の小数のゼロがある場合、それらの数字は結果文字列に含まれません。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、秒の 1/10 および 1/100 の桁を使用するかどうかはオプションです。

次の例では、"FF" カスタム書式指定子を使用して、TimeSpan 値の秒部分の 1/100 を表示します。 このカスタム書式指定子は解析操作でも使用されます。

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

表のトップへ

"FFF" カスタム書式指定子

"FFF" カスタム書式指定子 ("F" 文字が 3 つ) は、時間間隔の秒部分の 1/1000 を出力します。 書式指定操作では、これより下位の小数桁は切り捨てられます。 後続の小数のゼロがある場合、それらの数字は結果文字列に含まれません。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、秒の 1/10、1/100、および 1/1000 の桁を使用するかどうかはオプションです。

次の例では、"FFF" カスタム書式指定子を使用して、TimeSpan 値の秒部分の 1/1000 を表示します。 このカスタム書式指定子は解析操作でも使用されます。

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

表のトップへ

"FFFF" カスタム書式指定子

"FFFF" カスタム書式指定子 ("F" 文字が 4 つ) は、時間間隔の秒部分の 1/10000 を出力します。 書式指定操作では、これより下位の小数桁は切り捨てられます。 後続の小数のゼロがある場合、それらの数字は結果文字列に含まれません。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、秒の 1/10、1/100、1/1000、および 1/10000 の桁を使用するかどうかはオプションです。

次の例では、"FFFF" カスタム書式指定子を使用して、TimeSpan 値の秒部分の 1/10000 を表示します。 "FFFF" カスタム書式指定子は解析操作でも使用されます。

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

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

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

foreach (string input in inputs) {
   if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
      Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3);
   else
      Console.WriteLine("Cannot parse {0} with '{1}'.",
                        input, fmt);
}
// The example displays the following output:
//       Formatting:
//       00:00:03.6974900 ('FFFF') --> 6974
//       00:00:03.8000900 ('ss\.FFFF') --> 03.8
//
//       Parsing:
//       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
//       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
//       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
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" 文字が 5 つ) は、時間間隔の秒部分の 1/100000 を出力します。 書式指定操作では、これより下位の小数桁は切り捨てられます。 後続の小数のゼロがある場合、それらの数字は結果文字列に含まれません。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、秒の 1/10、1/100、1/1000、1/10000、および 1/100000 の桁を使用するかどうかはオプションです。

次の例では、"FFFFF" カスタム書式指定子を使用して、TimeSpan 値の秒部分の 1/100000 を表示します。 "FFFFF" カスタム書式指定子は解析操作でも使用されます。

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

表のトップへ

"FFFFFF" カスタム書式指定子

"FFFFFF" カスタム書式指定子 ("F" 文字が 6 つ) は、時間間隔の秒部分の 1/1000000 を出力します。 書式指定操作では、これより下位の小数桁は切り捨てられます。 後続の小数のゼロがある場合、それらの数字は結果文字列に含まれません。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、秒の 1/10、1/100、1/1000、1/10000、1/100000 および 1/1000000 の桁を使用するかどうかはオプションです。

次の例では、"FFFFFF" カスタム書式指定子を使用して、TimeSpan 値の秒部分の 1/1000000 を表示します。 このカスタム書式指定子は解析操作でも使用されます。

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

表のトップへ

"FFFFFFF" カスタム書式指定子

"FFFFFFF" カスタム書式指定子 ("F" 文字が 7 つ) は、時間間隔の秒部分の 1/10000000 (またはタイマー刻みの小数部) を出力します。 後続の小数のゼロがある場合、それらの数字は結果文字列に含まれません。 TimeSpan.ParseExact メソッドまたは TimeSpan.TryParseExact メソッドを呼び出す解析操作では、入力文字列の 7 桁の小数部を使用するかどうかはオプションです。

次の例では、"FFFFFFF" カスタム書式指定子を使用して、TimeSpan 値の秒部分の小数部を表示します。 このカスタム書式指定子は解析操作でも使用されます。

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     

表のトップへ

その他の文字

空白文字など、書式指定文字列内のエスケープされないその他の文字は、カスタム書式指定子として解釈されます。 ほとんどの場合、エスケープされないその他の文字が存在すると、FormatException が発生します。

書式指定文字列にリテラル文字を含める方法は 2 つあります。

  • 単一引用符 (リテラル文字列の区切り記号) で囲みます。

  • エスケープ文字として解釈される円記号 ("\") を前に付けます。 このため、C# では、書式指定文字列に @ を付けるか、リテラル文字の前に追加の円記号が指定されている必要があります。

    条件ロジックを使用してエスケープされたリテラルを書式指定文字列に含めることが必要になる場合もあります。 次の例では、条件ロジックを使用して負の時間間隔の符号を含めます。

    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 では、時間間隔の区切り記号の文法が定義されていません。 そのため、日と時間、時間と分、分と秒、および秒と秒の小数部の間の区切り記号は、書式指定文字列ですべて文字リテラルとして扱う必要があります。

次の例では、エスケープ文字と単一引用符の両方を使用して、出力文字列に "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      

表のトップへ

関連項目