次の方法で共有


標準の時間間隔書式指定文字列

標準の時間間隔 (TimeSpan) 書式指定文字列は、書式設定操作によって生成される TimeSpan 値のテキスト表現を、単一の書式指定子を使用して定義します。 空白を含む複数の文字で構成される書式指定文字列は、カスタム時間間隔 (TimeSpan) 書式指定文字列として解釈されます。 詳細については、「カスタム時間間隔書式指定文字列」を参照してください。

TimeSpan 値の文字列形式は、TimeSpan.ToString メソッドのオーバーロードの呼び出しと、String.Format などの複合書式指定をサポートするメソッドによって生成されます。 詳細については、「型の書式設定」および「複合書式設定」を参照してください。 次のコード例では、書式設定操作で標準書式指定文字列を使用する方法を示しています。

Module Example
   Public Sub Main()
      Dim duration As New TimeSpan(1, 12, 23, 62)
      Dim output As String = "Time of Travel: " + duration.ToString("c")
      Console.WriteLine(output)

      Console.WriteLine("Time of Travel: {0:c}", duration) 
   End Sub
End Module
' The example displays the following output:
'       Time of Travel: 1.12:24:02
'       Time of Travel: 1.12:24:02
using System;

public class Example
{
   public static void Main()
   {
      TimeSpan duration = new TimeSpan(1, 12, 23, 62);
      string output = "Time of Travel: " + duration.ToString("c");
      Console.WriteLine(output);

      Console.WriteLine("Time of Travel: {0:c}", duration); 
   }
}
// The example displays the following output:
//       Time of Travel: 1.12:24:02
//       Time of Travel: 1.12:24:02

標準の TimeSpan 書式指定文字列は、解析操作に必要な入力文字列の書式を定義するために TimeSpan.ParseExact メソッドと TimeSpan.TryParseExact メソッドでも使用されます (解析では、特定の値の文字列形式が、その値に変換されます)。 次のコード例では、解析操作で標準書式指定文字列を使用する方法を示しています。

Module Example
   Public Sub Main()
      Dim value As String = "1.03:14:56.1667"
      Dim interval As TimeSpan
      Try
         interval = TimeSpan.ParseExact(value, "c", Nothing)
         Console.WriteLine("Converted '{0}' to {1}", value, interval)
      Catch e As FormatException
         Console.WriteLine("{0}: Bad Format", value)
      Catch e As OverflowException
         Console.WriteLine("{0}: Out of Range", value)
      End Try

      If TimeSpan.TryParseExact(value, "c", Nothing, interval) Then
         Console.WriteLine("Converted '{0}' to {1}", value, interval)
      Else
         Console.WriteLine("Unable to convert {0} to a time interval.", 
                           value)
      End If                
   End Sub
End Module
' The example displays the following output:
'       Converted '1.03:14:56.1667' to 1.03:14:56.1667000
'       Converted '1.03:14:56.1667' to 1.03:14:56.1667000
using System;

public class Example
{
   public static void Main()
   {
      string value = "1.03:14:56.1667";
      TimeSpan interval;
      try {
         interval = TimeSpan.ParseExact(value, "c", null);
         Console.WriteLine("Converted '{0}' to {1}", value, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("{0}: Bad Format", value);
      }   
      catch (OverflowException) {
         Console.WriteLine("{0}: Out of Range", value);
      }

      if (TimeSpan.TryParseExact(value, "c", null, out interval))
         Console.WriteLine("Converted '{0}' to {1}", value, interval);
      else
         Console.WriteLine("Unable to convert {0} to a time interval.", 
                           value);
   }
}
// The example displays the following output:
//       Converted '1.03:14:56.1667' to 1.03:14:56.1667000
//       Converted '1.03:14:56.1667' to 1.03:14:56.1667000

標準時間間隔書式指定子を次の表に示します。

書式指定子

名前

説明

"c"

固定 (不変) 書式

この指定子はカルチャに依存しません。 形式は [-][d’.’]hh’:’mm’:’ss[‘.’fffffff] です。

("t" と "T" の書式指定文字列は同じ結果を生成します)。

詳細については、「固定 ("c") 書式指定子」を参照してください。

TimeSpan.Zero -> 00:00:00

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

New TimeSpan(3, 17, 25, 30, 500) -> 3.17:25:30.5000000

"g"

一般の短い書式

この指定子では、必要な結果のみが出力されます。 カルチャに依存し、[-][d’:’]h’:’mm’:’ss[.FFFFFFF] という形式になります。

詳細については、「一般の短い ("g") 書式指定子」を参照してください。

New TimeSpan(1, 3, 16, 50, 500) -> 1:3:16:50.5 (en-US)

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

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

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

"G"

一般の長い書式

この指定子では、常に日数と 7 桁の小数部の桁が出力されます。 カルチャに依存し、[-]d’:’hh’:’mm’:’ss.fffffff という形式になります。

詳細については、「一般の長い ("G") 書式指定子」を参照してください。

New TimeSpan(18, 30, 0) -> 0:18:30:00.0000000 (en-US)

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

固定 ("c") 書式指定子

"c" 書式指定子は、TimeSpan 値の文字列形式を次の書式で返します。

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

角かっこ ([ と ]) で囲まれている要素は省略可能です。 ピリオド (.) およびコロン (:) はリテラル文字です。 その他の要素について次の表で説明します。

要素

説明

-

負の時間間隔を示す省略可能なマイナス記号。

d

先行ゼロを付けない、省略可能な日数。

hh

時間数 ("00" ~ "23")。

mm

分数 ("00" ~ "59")。

ss

秒数 ("00" ~ "59")。

fffffff

省略可能なミリ秒数 ("0010000" ~ "9990000")。

"g" や "G" の書式指定子とは異なり、"c" 書式指定子はカルチャに依存しません。 不変であり、.NET Framework Version 4 より前のすべてのバージョンの .NET Framework に共通の、TimeSpan 値の文字列形式を生成します。 " c" は既定の TimeSpan 書式指定文字列です。TimeSpan.ToString() メソッドは、"c" 書式指定文字列を使用して時間間隔値を書式指定します。

メモメモ

TimeSpan では、"c" の標準書式指定文字列と同じ動作をする標準書式指定文字列 "t" および "T" もサポートされます。

次の例では、2 つの TimeSpan オブジェクトをインスタンス化し、それらを使用して算術演算を実行し、結果を表示します。 いずれの場合も、複合書式指定を使用し、"c" 書式指定子を使用して TimeSpan 値を表示します。

Module Example
   Public Sub Main()
      Dim interval1, interval2 As TimeSpan
      interval1 = New TimeSpan(7, 45, 16)
      interval2 = New TimeSpan(18, 12, 38)

      Console.WriteLine("{0:c} - {1:c} = {2:c}", interval1, 
                        interval2, interval1 - interval2)
      Console.WriteLine("{0:c} + {1:c} = {2:c}", interval1, 
                        interval2, interval1 + interval2)

      interval1 = New TimeSpan(0, 0, 1, 14, 365)
      interval2 = TimeSpan.FromTicks(2143756)      
      Console.WriteLine("{0:c} + {1:c} = {2:c}", interval1, 
                        interval2, interval1 + interval2)
   End Sub
End Module
' The example displays the following output:
'       07:45:16 - 18:12:38 = -10:27:22
'       07:45:16 + 18:12:38 = 1.01:57:54
'       00:01:14.3650000 + 00:00:00.2143756 = 00:01:14.5793756
using System;

public class Example
{
   public static void Main()
   {
      TimeSpan interval1, interval2;
      interval1 = new TimeSpan(7, 45, 16);
      interval2 = new TimeSpan(18, 12, 38);

      Console.WriteLine("{0:c} - {1:c} = {2:c}", interval1, 
                        interval2, interval1 - interval2);
      Console.WriteLine("{0:c} + {1:c} = {2:c}", interval1, 
                        interval2, interval1 + interval2);

      interval1 = new TimeSpan(0, 0, 1, 14, 365);
      interval2 = TimeSpan.FromTicks(2143756);  
      Console.WriteLine("{0:c} + {1:c} = {2:c}", interval1, 
                        interval2, interval1 + interval2);
   }
}
// The example displays the following output:
//       07:45:16 - 18:12:38 = -10:27:22
//       07:45:16 + 18:12:38 = 1.01:57:54
//       00:01:14.3650000 + 00:00:00.2143756 = 00:01:14.5793756

表のトップへ

一般の短い ("g") 書式指定子

"g" TimeSpan 書式指定子は、必要な要素のみを含めることによって、TimeSpan 値の文字列形式を簡潔な書式で返します。 その形式は次のとおりです。

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

角かっこ ([ と ]) で囲まれている要素は省略可能です。 コロン (:) はリテラル文字です。 その他の要素について次の表で説明します。

要素

説明

-

負の時間間隔を示す省略可能なマイナス記号。

d

先行ゼロを付けない、省略可能な日数。

h

先行ゼロを付けない時間数 ("0" ~ "23")。

mm

分数 ("00" ~ "59")。

ss

秒数 ("00" ~ "59")。

.

秒の小数部の区切り記号。 ユーザーがオーバーライドしていない、指定したカルチャの NumberDecimalSeparator プロパティと同じです。

FFFFFFF

秒の小数部。 できるだけ少ない桁数が表示されます。

"G" 書式指定子と同様に、"g" 書式指定子はローカライズされます。 秒の小数部の区切り記号は、現在のカルチャまたは特定のカルチャの NumberDecimalSeparator プロパティに基づきます。

次の例では、2 つの TimeSpan オブジェクトをインスタンス化し、それらを使用して算術演算を実行し、結果を表示します。 いずれの場合も、複合書式指定を使用し、"g" 書式指定子を使用して TimeSpan 値を表示します。 また、現在のシステム カルチャ (この場合は英語 - 米国、すなわち、en-US) とフランス語 - フランス (fr-FR) カルチャの書式指定規則を使用して、TimeSpan 値を書式指定します。

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim interval1, interval2 As TimeSpan
      interval1 = New TimeSpan(7, 45, 16)
      interval2 = New TimeSpan(18, 12, 38)

      Console.WriteLine("{0:g} - {1:g} = {2:g}", interval1, 
                        interval2, interval1 - interval2)
      Console.WriteLine(String.Format(New CultureInfo("fr-FR"), 
                        "{0:g} + {1:g} = {2:g}", interval1, 
                        interval2, interval1 + interval2))

      interval1 = New TimeSpan(0, 0, 1, 14, 36)
      interval2 = TimeSpan.FromTicks(2143756)      
      Console.WriteLine("{0:g} + {1:g} = {2:g}", interval1, 
                        interval2, interval1 + interval2)
   End Sub
End Module
' The example displays the following output:
'       7:45:16 - 18:12:38 = -10:27:22
'       7:45:16 + 18:12:38 = 1:1:57:54
'       0:01:14.036 + 0:00:00.2143756 = 0:01:14.2503756
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      TimeSpan interval1, interval2;
      interval1 = new TimeSpan(7, 45, 16);
      interval2 = new TimeSpan(18, 12, 38);

      Console.WriteLine("{0:g} - {1:g} = {2:g}", interval1, 
                        interval2, interval1 - interval2);
      Console.WriteLine(String.Format(new CultureInfo("fr-FR"), 
                        "{0:g} + {1:g} = {2:g}", interval1, 
                        interval2, interval1 + interval2));

      interval1 = new TimeSpan(0, 0, 1, 14, 36);
      interval2 = TimeSpan.FromTicks(2143756);      
      Console.WriteLine("{0:g} + {1:g} = {2:g}", interval1, 
                        interval2, interval1 + interval2);
   }
}
// The example displays the following output:
//       7:45:16 - 18:12:38 = -10:27:22
//       7:45:16 + 18:12:38 = 1:1:57:54
//       0:01:14.036 + 0:00:00.2143756 = 0:01:14.2503756

表のトップへ

一般の長い ("G") 書式指定子

"G" TimeSpan 書式指定子は、TimeSpan 値の文字列形式を、日数と秒の小数部の両方を常に含む長い書式で返します。 "G" 標準書式指定子の結果の文字列の書式を次に示します。

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

角かっこ ([ と ]) で囲まれている要素は省略可能です。 コロン (:) はリテラル文字です。 その他の要素について次の表で説明します。

要素

説明

-

負の時間間隔を示す省略可能なマイナス記号。

d

先行ゼロを付けない日数。

hh

時間数 ("00" ~ "23")。

mm

分数 ("00" ~ "59")。

ss

秒数 ("00" ~ "59")。

.

秒の小数部の区切り記号。 ユーザーがオーバーライドしていない、指定したカルチャの NumberDecimalSeparator プロパティと同じです。

fffffff

秒の小数部。

"G" 書式指定子と同様に、"g" 書式指定子はローカライズされます。 秒の小数部の区切り記号は、現在のカルチャまたは特定のカルチャの NumberDecimalSeparator プロパティに基づきます。

次の例では、2 つの TimeSpan オブジェクトをインスタンス化し、それらを使用して算術演算を実行し、結果を表示します。 いずれの場合も、複合書式指定を使用し、"G" 書式指定子を使用して TimeSpan 値を表示します。 また、現在のシステム カルチャ (この場合は英語 - 米国、すなわち、en-US) とフランス語 - フランス (fr-FR) カルチャの書式指定規則を使用して、TimeSpan 値を書式指定します。

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim interval1, interval2 As TimeSpan
      interval1 = New TimeSpan(7, 45, 16)
      interval2 = New TimeSpan(18, 12, 38)

      Console.WriteLine("{0:G} - {1:G} = {2:G}", interval1, 
                        interval2, interval1 - interval2)
      Console.WriteLine(String.Format(New CultureInfo("fr-FR"), 
                        "{0:G} + {1:G} = {2:G}", interval1, 
                        interval2, interval1 + interval2))

      interval1 = New TimeSpan(0, 0, 1, 14, 36)
      interval2 = TimeSpan.FromTicks(2143756)      
      Console.WriteLine("{0:G} + {1:G} = {2:G}", interval1, 
                        interval2, interval1 + interval2)
   End Sub
End Module
' The example displays the following output:
'       0:07:45:16.0000000 - 0:18:12:38.0000000 = -0:10:27:22.0000000
'       0:07:45:16,0000000 + 0:18:12:38,0000000 = 1:01:57:54,0000000
'       0:00:01:14.0360000 + 0:00:00:00.2143756 = 0:00:01:14.2503756
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      TimeSpan interval1, interval2;
      interval1 = new TimeSpan(7, 45, 16);
      interval2 = new TimeSpan(18, 12, 38);

      Console.WriteLine("{0:G} - {1:G} = {2:G}", interval1, 
                        interval2, interval1 - interval2);
      Console.WriteLine(String.Format(new CultureInfo("fr-FR"), 
                        "{0:G} + {1:G} = {2:G}", interval1, 
                        interval2, interval1 + interval2));

      interval1 = new TimeSpan(0, 0, 1, 14, 36);
      interval2 = TimeSpan.FromTicks(2143756);      
      Console.WriteLine("{0:G} + {1:G} = {2:G}", interval1, 
                        interval2, interval1 + interval2);
   }
}
// The example displays the following output:
//       0:07:45:16.0000000 - 0:18:12:38.0000000 = -0:10:27:22.0000000
//       0:07:45:16,0000000 + 0:18:12:38,0000000 = 1:01:57:54,0000000
//       0:00:01:14.0360000 + 0:00:00:00.2143756 = 0:00:01:14.2503756

表のトップへ

参照

概念

型の書式設定

その他の技術情報

カスタム時間間隔書式指定文字列

文字列の解析