ParseExact 方法 (String, String, IFormatProvider)
本文為機器翻譯文章。如需檢視英文版,請選取 [原文] 核取方塊。您也可以將滑鼠指標移到文字上,即可在快顯視窗顯示英文原文。
譯文
原文

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

對應至 input 的時間間隔,如 formatformatProvider 所指定。

Exception Condition
ArgumentNullException

inputnull

FormatException

input 具有無效的格式。

OverflowException

input 代表小於 TimeSpan.MinValue 或大於 TimeSpan.MaxValue 的數字。

-或-

input 中的天數、小時、分鐘或秒鐘元件中,至少有一個元件超出有效範圍。

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 起可用
回到頁首
顯示:
© 2016 Microsoft