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

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

Overloads Public Shared Function ParseExact( _
   ByVal s As String, _   ByVal format As String, _   ByVal provider As IFormatProvider _) As DateTime
[C#]
public static DateTime ParseExact(strings,stringformat,IFormatProviderprovider);
[C++]
public: static DateTime ParseExact(String* s,String* format,IFormatProvider* provider);
[JScript]
public static function ParseExact(
   s : String,format : String,provider : IFormatProvider) : DateTime;

パラメータ

  • s
    変換する日付と時刻を格納した文字列。
  • format
    s の必要な書式。
  • provider
    s に関するカルチャ固有の書式情報を提供する IFormatProvider

戻り値

formatprovider で指定されたとおりの、 s に格納されている日付と時刻に等価の DateTime

例外

例外の種類 条件
ArgumentNullException s または format が null 参照 (Visual Basic では Nothing) です。
FormatException s または format が空の文字列です。

または

s に、 format で指定されたパターンに対応する日付と時刻が格納されていません。

解説

このメソッドは、 s の書式が完全に format の書式パターンで指定されたとおりでない場合は、 FormatException をスローします。 format が単一の標準書式指定文字から成る場合は、その文字で表された書式パターンが使用されます。詳細については、 DateTimeFormatInfo のトピックを参照してください。

s に時刻だけを指定して日付を指定しない場合は、現在の日付が使用されます。 s に日付だけを指定して時刻を指定しない場合は、深夜 (00:00:00) が使用されます。

パラメータ s 内の先頭、内部、または末尾に空白文字を含めることはできません。

パラメータ provider により、カルチャ固有の日付と時刻の形式指定情報が指定されます。たとえば、特定の言語における曜日の名前、または、月、日、年の優先表示順序。 provider が null 参照 (Visual Basic では Nothing) の場合は、現在のカルチャが使用されます。

使用例

[Visual Basic, C#, C++] ParseExact メソッドを次のサンプルで示します。

 
Imports System
Imports System.Globalization

Class Class1
   Public Shared Sub Main()
      ' Assume the current culture is en-US. 
      ' The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.

      Dim myDateTimeValue As String = "2/16/1992 12:15:12"
      Dim myDateTime As DateTime = DateTime.Parse(myDateTimeValue)
      Console.WriteLine("1) myDateTime       = {0}", myDateTime)
      
      ' Reverse month and day to conform to a different culture.
      ' The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.

      Dim culture = New CultureInfo("fr-FR", True)
      Dim myDateTimeFrenchValue As String = "    16/02/1992 12:15:12"
      Dim myDateTimeFrench As DateTime = _
                           DateTime.Parse(myDateTimeFrenchValue, _
                                          culture, _
                                          DateTimeStyles.NoCurrentDateDefault)
      Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench)
      
      ' The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.

      Dim expectedFormats As String() =  {"G", "g", "f", "F"}
      myDateTimeFrench = DateTime.ParseExact(myDateTimeFrenchValue, _
                                          expectedFormats, _
                                          culture, _
                                          DateTimeStyles.AllowWhiteSpaces)
      Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench)
   End Sub 'Main
End Class 'Class1
'
'This example yields the following results:
'
'1) myDateTime       = 2/16/1992 12:15:12 PM
'2) myDateTimeFrench = 2/16/1992 12:15:12 PM
'3) myDateTimeFrench = 2/16/1992 12:15:12 PM
'

[C#] 
using System;
using System.Globalization;

namespace Parse
{
    class Class1
    {
        public static void Main(string[] args)
        {
// Assume the current culture is en-US. 
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.

        string myDateTimeValue = "2/16/1992 12:15:12";
        DateTime myDateTime = DateTime.Parse(myDateTimeValue);
        Console.WriteLine("1) myDateTime       = {0}", myDateTime);

// Reverse month and day to conform to a different culture.
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.

        IFormatProvider culture = new CultureInfo("fr-FR", true);
        string myDateTimeFrenchValue = "    16/02/1992 12:15:12";
        DateTime myDateTimeFrench =
            DateTime.Parse(myDateTimeFrenchValue,
                           culture,
                           DateTimeStyles.NoCurrentDateDefault);
        Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench);
    
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.

        string[] expectedFormats = {"G", "g", "f" ,"F"};
        myDateTimeFrench = 
                DateTime.ParseExact(myDateTimeFrenchValue,
                                    expectedFormats,
                                    culture,
                                    DateTimeStyles.AllowWhiteSpaces);
        Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench);
        }
    }
}
/*
This example yields the following results:

1) myDateTime       = 2/16/1992 12:15:12 PM
2) myDateTimeFrench = 2/16/1992 12:15:12 PM
3) myDateTimeFrench = 2/16/1992 12:15:12 PM
*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

int main() {
   // Assume the current culture is en-US.
   // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.

   String* myDateTimeValue = S"2/16/1992 12:15:12";
   DateTime myDateTime = DateTime::Parse(myDateTimeValue);
   Console::WriteLine(S"1) myDateTime       = {0}", __box(myDateTime));

   // Reverse month and day to conform to a different culture.
   // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.

   IFormatProvider* culture = new CultureInfo(S"fr-FR", true);
   String* myDateTimeFrenchValue = S"    16/02/1992 12:15:12";
   DateTime myDateTimeFrench =
      DateTime::Parse(myDateTimeFrenchValue,
      culture,
      DateTimeStyles::NoCurrentDateDefault);
      Console::WriteLine(S"2) myDateTimeFrench = {0}", __box(myDateTimeFrench));

   // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.

   String* expectedFormats[] = {S"G", S"g", S"f" , S"F"};
   myDateTimeFrench =
      DateTime::ParseExact(myDateTimeFrenchValue,
      expectedFormats,
      culture,
      DateTimeStyles::AllowWhiteSpaces);
      Console::WriteLine(S"3) myDateTimeFrench = {0}", __box(myDateTimeFrench));
}
/*
This example yields the following results:

1) myDateTime       = 2/16/1992 12:15:12 PM
2) myDateTimeFrench = 2/16/1992 12:15:12 PM
3) myDateTimeFrench = 2/16/1992 12:15:12 PM
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

DateTime 構造体 | DateTime メンバ | System 名前空間 | DateTime.ParseExact オーバーロードの一覧 | 書式設定の概要 | 日付と時刻の書式指定文字列 | Parse | String | CultureInfo | CurrentInfo