DateTime.ParseExact メソッド

指定した文字列形式の日付と時刻を等価の DateTime の値に変換します。文字列形式の書式は、指定した書式と完全に一致する必要があります。

オーバーロードの一覧

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function ParseExact(String, String, IFormatProvider) As DateTime

[C#] public static DateTime ParseExact(string, string, IFormatProvider);

[C++] public: static DateTime ParseExact(String*, String*, IFormatProvider*);

[JScript] public static function ParseExact(String, String, IFormatProvider) : DateTime;

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function ParseExact(String, String, IFormatProvider, DateTimeStyles) As DateTime

[C#] public static DateTime ParseExact(string, string, IFormatProvider, DateTimeStyles);

[C++] public: static DateTime ParseExact(String*, String*, IFormatProvider*, DateTimeStyles);

[JScript] public static function ParseExact(String, String, IFormatProvider, DateTimeStyles) : DateTime;

指定した書式の配列、カルチャ固有の書式情報、およびスタイルを使用して、指定した日付と時刻の文字列形式を等価の DateTime に変換します。文字列形式の書式は、指定した書式の少なくとも 1 つと完全に一致する必要があります。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function ParseExact(String, String(), IFormatProvider, DateTimeStyles) As DateTime

[C#] public static DateTime ParseExact(string, string[], IFormatProvider, DateTimeStyles);

[C++] public: static DateTime ParseExact(String*, String*[], IFormatProvider*, DateTimeStyles);

[JScript] public static function ParseExact(String, String[], IFormatProvider, DateTimeStyles) : DateTime;

使用例

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

[Visual Basic, C#, C++] メモ   ここでは、ParseExact のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

DateTime 構造体 | DateTime メンバ | System 名前空間