この記事は機械翻訳されています。英語版の記事を表示するには、[英語] のチェック ボックスをオンにしてください。また、テキストにマウス ポインターを合わせると、ポップアップ ウィンドウに英語のテキストを表示することもできます。
翻訳
英語

TimeSpan.ParseExact メソッド (String, String, IFormatProvider)

 

指定した書式とカルチャ固有の書式情報を使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。 文字列形式の書式は、指定した書式と完全に一致する必要があります。

名前空間:   System
アセンブリ:  mscorlib (mscorlib.dll 内)

public static TimeSpan ParseExact(
	string input,
	string format,
	IFormatProvider formatProvider
)

パラメーター

input
Type: System.String

変換する時間間隔を指定する文字列。

format
Type: System.String

input で必要とされる書式を定義する標準またはカスタムの書式指定文字列。

formatProvider
Type: System.IFormatProvider

カルチャ固有の書式情報を提供するオブジェクト。

戻り値

Type: System.TimeSpan

format および formatProvider の指定に従って変換された、input に対応する時間間隔。

Exception Condition
ArgumentNullException

inputnull です。

FormatException

input の形式は無効です。

OverflowException

inputTimeSpan.MinValue 未満の数値か、TimeSpan.MaxValue より大きい数値を表します。

または

input の日、時間、分、または秒のコンポーネントのうち少なくとも 1 つが、その有効範囲外です。

The M:System.TimeSpan.ParseExact(System.String,System.String,System.IFormatProvider) method parses the string representation of a time interval, which must be in the format defined by the format parameter, except that leading and trailing white-space characters are ignored. Because input must conform to the format of format exactly, you should always use exception handling when converting a string input by the user to a time interval. If you prefer not to use exception handling, you can call the M:System.TimeSpan.TryParseExact(System.String,System.String,System.IFormatProvider,System.TimeSpan@) method instead.

The format parameter is a string that contains either a single standard format specifier, or one or more custom format specifiers that define the required format of input. For more information about valid format strings, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings.

System_CAPS_important重要

The M:System.TimeSpan.ParseExact(System.String,System.String,System.IFormatProvider) method uses the conventions of the culture specified by the formatProvider parameter only if format is a standard T:System.TimeSpan format string whose value is either "g" or "G". The "c", "t", and "T" standard format strings use the formatting conventions of the invariant culture. Custom format strings define the precise format of the input string and use literal characters to separate the components of a time interval.

The formatProvider parameter is an T:System.IFormatProvider implementation that provides culture-specific information about the format of the returned string if format is a standard format string. The formatProvider parameter can be any of the following:

  • A T:System.Globalization.CultureInfo object that represents the culture whose formatting conventions are to be reflected in the returned string. The T:System.Globalization.DateTimeFormatInfo object returned by the P:System.Globalization.CultureInfo.DateTimeFormat property defines the formatting of the returned string.

  • A T:System.Globalization.DateTimeFormatInfo object that defines the formatting of the returned string.

  • A custom object that implements the T:System.IFormatProvider interface. Its M:System.IFormatProvider.GetFormat(System.Type) method returns a T:System.Globalization.DateTimeFormatInfo object that provides formatting information.

If formatProvider is null, the T:System.Globalization.DateTimeFormatInfo object that is associated with the current culture is used.

The following example uses the M:System.TimeSpan.ParseExact(System.String,System.String,System.IFormatProvider) method to parse several string representations of time intervals using various format strings and cultures.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string intervalString, format;
      TimeSpan interval;
      CultureInfo culture;

      // Parse hour:minute value with "g" specifier current culture.
      intervalString = "17:14";
      format = "g";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", 
                           intervalString, format);
      }                     
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      }      

      // Parse hour:minute:second value with "G" specifier.
      intervalString = "17:14:48";
      format = "G";
      culture = CultureInfo.InvariantCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse days:hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "3:17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48.153";
      format = "G";
      culture = new CultureInfo("fr-FR");
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48,153";
      format = "G";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse a single number using the "c" standard format string. 
      intervalString = "12";
      format = "c";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, null);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse a single number using the "%h" custom format string. 
      format = "%h";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, null);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse a single number using the "%s" custom format string. 
      format = "%s";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, null);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      }
   }
}
// The example displays the following output:
//       '17:14' --> 17:14:00
//       '17:14:48': Bad Format for 'G'
//       '17:14:48.153': Bad Format for 'G'
//       '3:17:14:48.153' --> 3.17:14:48.1530000
//       '3:17:14:48.153': Bad Format for 'G'
//       '3:17:14:48,153' --> 3.17:14:48.1530000
//       '12' --> 12.00:00:00
//       '12' --> 12:00:00
//       '12' --> 00:00:12

Universal Windows Platform
4.5 から利用可能
.NET Framework
4.0 から利用可能
Portable Class Library
サポート対象 : portable .NET platforms
Silverlight
4.0 から利用可能
Windows Phone Silverlight
7.1 から利用可能
Windows Phone
8.1 から利用可能
トップに戻る
表示: