DateTime.ParseExact メソッド

定義

指定した文字列形式の日付と時刻を等価の DateTime の値に変換します。 文字列形式の書式は、指定した書式と完全に一致する必要があります。それ以外の場合は、例外がスローされます。

オーバーロード

ParseExact(String, String, IFormatProvider)

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

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

指定した書式、カルチャ固有の書式情報、スタイルを使用して、指定した日付と時刻のスパン表現を、それと等価な DateTime に変換します。 文字列形式の書式は、指定した書式と完全に一致する必要があります。それ以外の場合は、例外がスローされます。

ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)

指定した書式の配列、カルチャ固有の書式情報、スタイルを使用して、指定した日付と時刻のスパン表現を、それと等価な DateTime に変換します。 文字列形式の書式は、指定した書式の少なくとも 1 つと完全に一致する必要があります。それ以外の場合は、例外がスローされます。

ParseExact(String, String, IFormatProvider, DateTimeStyles)

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

ParseExact(String, String[], IFormatProvider, DateTimeStyles)

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

注釈

重要

和暦の時代 (年号) は天皇の代に基づいているため、変更されることが予想されます。 たとえば、JapaneseCalendarJapaneseLunisolarCalendar において、2019 年 5 月 1 日から令和時代が始まることになりました。 このような時代 (年号) の変更は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細と、アプリケーションが影響を受けるかどうかを判断するには、「 .NET での日本語のカレンダーでの新しい時代 (年号の処理)」を参照してください。 Windows システムでアプリケーションをテストして時代 (年号) の変更に対する準備を確認する方法については、「 日本の時代 (年号) の変更に備える」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。

ParseExact(String, String, IFormatProvider)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

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

public:
 static DateTime ParseExact(System::String ^ s, System::String ^ format, IFormatProvider ^ provider);
public static DateTime ParseExact (string s, string format, IFormatProvider provider);
public static DateTime ParseExact (string s, string format, IFormatProvider? provider);
static member ParseExact : string * string * IFormatProvider -> DateTime
Public Shared Function ParseExact (s As String, format As String, provider As IFormatProvider) As DateTime

パラメーター

s
String

変換する日時を含む文字列。

format
String

s の必要な形式を定義する形式指定子。 詳細については、「解説」を参照してください。

provider
IFormatProvider

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

戻り値

format および provider で指定された書式に従って表記された s に含まれる日時と等価のオブジェクト。

例外

s または formatnull です。

s または format が空の文字列です。

または

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

または

s の時間の部分と AM/PM 指定子が一致していません。

ParseExactメソッドの例を次に示します。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string dateString, format;
      DateTime result;
      CultureInfo provider = CultureInfo.InvariantCulture;

      // Parse date-only value with invariant culture.
      dateString = "06/15/2008";
      format = "d";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }

      // Parse date-only value without leading zero in month using "d" format.
      // Should throw a FormatException because standard short date pattern of
      // invariant culture requires two-digit month.
      dateString = "6/15/2008";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }

      // Parse date and time with custom specifier.
      dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
      format = "ddd dd MMM yyyy h:mm tt zzz";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }

      // Parse date and time with offset but without offset's minutes.
      // Should throw a FormatException because "zzz" specifier requires leading
      // zero in hours.
      dateString = "Sun 15 Jun 2008 8:30 AM -06";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }

      dateString = "15/06/2008 08:30";
      format = "g";
      provider = new CultureInfo("fr-FR");
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }

      // Parse a date that includes seconds and milliseconds
      // by using the French (France) and invariant cultures.
      dateString = "18/08/2015 06:30:15.006542";
      format = "dd/MM/yyyy HH:mm:ss.ffffff";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }
   }
}
// The example displays the following output:
//       06/15/2008 converts to 6/15/2008 12:00:00 AM.
//       6/15/2008 is not in the correct format.
//       Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 7:30:00 AM.
//       Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.
//       15/06/2008 08:30 converts to 6/15/2008 8:30:00 AM.
//       18/08/2015 06:30:15.006542 converts to 8/18/2015 6:30:15 AM.
open System
open System.Globalization

[<EntryPoint>]
let main _ =
    let provider = CultureInfo.InvariantCulture

    // Parse date-only value with invariant culture.
    let dateString = "06/15/2008"
    let format = "d"
    try
        let result = DateTime.ParseExact(dateString, format, provider)
        printfn $"{dateString} converts to {result}."
    with :? FormatException ->
        printfn $"{dateString} is not in the correct format."

    // Parse date-only value without leading zero in month using "d" format.
    // Should throw a FormatException because standard short date pattern of
    // invariant culture requires two-digit month.
    let dateString = "6/15/2008"
    try
        let result = DateTime.ParseExact(dateString, format, provider)
        printfn $"{dateString} converts to {result}."
    with :? FormatException ->
        printfn $"{dateString} is not in the correct format."

    // Parse date and time with custom specifier.
    let dateString = "Sun 15 Jun 2008 8:30 AM -06:00"
    let format = "ddd dd MMM yyyy h:mm tt zzz"
    try
        let result = DateTime.ParseExact(dateString, format, provider)
        printfn $"{dateString} converts to {result}."
    with :? FormatException ->
        printfn $"{dateString} is not in the correct format."

    // Parse date and time with offset but without offset's minutes.
    // Should throw a FormatException because "zzz" specifier requires leading
    // zero in hours.
    let dateString = "Sun 15 Jun 2008 8:30 AM -06"
    try
        let result = DateTime.ParseExact(dateString, format, provider)
        printfn $"{dateString} converts to {result}."
    with :? FormatException ->
        printfn $"{dateString} is not in the correct format."

    let dateString = "15/06/2008 08:30"
    let format = "g"
    let provider = CultureInfo "fr-FR"
    try
        let result = DateTime.ParseExact(dateString, format, provider)
        printfn $"{dateString} converts to {result}."
    with :? FormatException ->
        printfn $"{dateString} is not in the correct format."

    // Parse a date that includes seconds and milliseconds
    // by using the French (France) and invariant cultures.
    let dateString = "18/08/2015 06:30:15.006542"
    let format = "dd/MM/yyyy HH:mm:ss.ffffff"
    try
        let result = DateTime.ParseExact(dateString, format, provider)
        printfn $"{dateString} converts to {result}."
    with :? FormatException ->
        printfn $"{dateString} is not in the correct format."

    0

// The example displays the following output:
//       06/15/2008 converts to 6/15/2008 12:00:00 AM.
//       6/15/2008 is not in the correct format.
//       Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 7:30:00 AM.
//       Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.
//       15/06/2008 08:30 converts to 6/15/2008 8:30:00 AM.
//       18/08/2015 06:30:15.006542 converts to 8/18/2015 6:30:15 AM.
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim dateString, format As String  
      Dim result As Date
      Dim provider As CultureInfo = CultureInfo.InvariantCulture

      ' Parse date-only value with invariant culture.
      dateString = "06/15/2008"
      format = "d"
      Try
         result = Date.ParseExact(dateString, format, provider)
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
      Catch e As FormatException
         Console.WriteLine("{0} is not in the correct format.", dateString)
      End Try 

      ' Parse date-only value without leading zero in month using "d" format.
      ' Should throw a FormatException because standard short date pattern of 
      ' invariant culture requires two-digit month.
      dateString = "6/15/2008"
      Try
         result = Date.ParseExact(dateString, format, provider)
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
      Catch e As FormatException
         Console.WriteLine("{0} is not in the correct format.", dateString)
      End Try 
      
      ' Parse date and time with custom specifier.
      dateString = "Sun 15 Jun 2008 8:30 AM -06:00"
      format = "ddd dd MMM yyyy h:mm tt zzz"        
      Try
         result = Date.ParseExact(dateString, format, provider)
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
      Catch e As FormatException
         Console.WriteLine("{0} is not in the correct format.", dateString)
      End Try 
      
      ' Parse date and time with offset but without offset's minutes.
      ' Should throw a FormatException because "zzz" specifier requires leading  
      ' zero in hours.
      dateString = "Sun 15 Jun 2008 8:30 AM -06"
      Try
         result = Date.ParseExact(dateString, format, provider)
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
      Catch e As FormatException
         Console.WriteLine("{0} is not in the correct format.", dateString)
      End Try 
      
      ' Parse a date string using the French (France) culture.
      dateString = "15/06/2008 08:30"
      format = "g"
      provider = New CultureInfo("fr-FR")
      Try
         result = Date.ParseExact(dateString, format, provider)
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
      Catch e As FormatException
         Console.WriteLine("{0} is not in the correct format.", dateString)
      End Try

      ' Parse a date that includes seconds and milliseconds
      ' by using the French (France) and invariant cultures.
      dateString = "18/08/2015 06:30:15.006542"
      format = "dd/MM/yyyy HH:mm:ss.ffffff"
      Try
         result = Date.ParseExact(dateString, format, provider)
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
      Catch e As FormatException
         Console.WriteLine("{0} is not in the correct format.", dateString)
      End Try
   End Sub
End Module
' The example displays the following output:
'       06/15/2008 converts to 6/15/2008 12:00:00 AM.
'       6/15/2008 is not in the correct format.
'       Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 7:30:00 AM.
'       Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.
'       15/06/2008 08:30 converts to 6/15/2008 8:30:00 AM.
'       18/08/2015 06:30:15.006542 converts to 8/18/2015 6:30:15 AM.

注釈

メソッドは DateTime.ParseExact(String, String, IFormatProvider) 、日付の文字列表現を解析します。これは、 パラメーターで定義された format 形式である必要があります。 また、日付と時刻の<文字列表現の Date> 要素と <Time> 要素が、 でformats指定された順序で表示され、 で許可されているformat空白以外の空白が含まれない必要があります。 time 要素のない日付を定義し、解析操作が成功した場合 format 、結果 DateTime の値の時刻は午前 0 時 (00:00:00) になります。 日付要素のない時刻を定義し、解析操作が成功した場合 format 、結果 DateTime の値の日付 DateTime.Now.Dateは になります。

が特定のタイム ゾーンの時刻を表せず、解析操作が成功した場合s、返されるDateTime値の プロパティは ですDateTimeKind.UnspecifiedKind。 が特定のタイム ゾーンの時刻を表し、タイム ゾーン情報が存在することを許可する場合 s (たとえば、 format が "o"、"r"、または "u" 標準書式指定子と等しい場合、または "z"、"zz"、または "zzz" カスタム書式指定子が含まれている場合)Kind、返されるDateTime値のプロパティは ですDateTimeKind.Localformat

パラメーターは format 、1 つの標準書式指定子、または の必要な形式を定義する 1 つ以上の sカスタム書式指定子を含む文字列です。 有効な書式設定コードの詳細については、「 標準の日付と時刻の書式指定文字列 」または 「ユーザー設定の日付と時刻の書式指定文字列」を参照してください。

注意

が日付または時刻の区切り記号 ("yyyyMMddHHmm" など) を含まないカスタム書式パターンの場合 format は、 パラメーターのインバリアント カルチャ provider と、各カスタム書式指定子の最も広い形式を使用します。 たとえば、書式パターンで時間を指定する場合は、より狭い形式の "H" ではなく、より広い形式 "HH" を指定します。

で使用されるs特定の日付と時刻の記号と文字列 (特定の言語の曜日の名前など) は、 パラメーターによってprovider定義されます。これは、 が標準書式指定子文字列である場合formats正確な形式です。 providerパラメーターには、次のいずれかを指定できます。

nullの場合providerは、CultureInfo現在のカルチャに対応する オブジェクトが使用されます。

注意 (呼び出し元)

.NET Framework 4 では、ParseExact解析される文字列に時間コンポーネントと、一致しない AM/PM 指定子が含まれている場合、メソッドは をスローFormatExceptionします。 .NET Framework 3.5 以前のバージョンでは、AM/PM 指定子は無視されます。

こちらもご覧ください

適用対象

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

指定した書式、カルチャ固有の書式情報、スタイルを使用して、指定した日付と時刻のスパン表現を、それと等価な DateTime に変換します。 文字列形式の書式は、指定した書式と完全に一致する必要があります。それ以外の場合は、例外がスローされます。

public static DateTime ParseExact (ReadOnlySpan<char> s, ReadOnlySpan<char> format, IFormatProvider? provider, System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.None);
public static DateTime ParseExact (ReadOnlySpan<char> s, ReadOnlySpan<char> format, IFormatProvider provider, System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.None);
static member ParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.DateTimeStyles -> DateTime
Public Shared Function ParseExact (s As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), provider As IFormatProvider, Optional style As DateTimeStyles = System.Globalization.DateTimeStyles.None) As DateTime

パラメーター

s
ReadOnlySpan<Char>

変換する日付と時刻を表す文字を含むスパン。

format
ReadOnlySpan<Char>

s の必要な書式を定義する書式指定子を表す文字を含んでいるスパン。

provider
IFormatProvider

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

style
DateTimeStyles

ss に指定できるスタイル要素、または s から DateTime 値への変換に関する追加情報を提供する列挙値のビットごとの組み合わせ。 通常指定する値は、None です。

戻り値

formatprovider、および style で指定された書式に従って表記された s に含まれる日時と等価のオブジェクト。

適用対象

ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

指定した書式の配列、カルチャ固有の書式情報、スタイルを使用して、指定した日付と時刻のスパン表現を、それと等価な DateTime に変換します。 文字列形式の書式は、指定した書式の少なくとも 1 つと完全に一致する必要があります。それ以外の場合は、例外がスローされます。

public static DateTime ParseExact (ReadOnlySpan<char> s, string[] formats, IFormatProvider? provider, System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.None);
public static DateTime ParseExact (ReadOnlySpan<char> s, string[] formats, IFormatProvider provider, System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.None);
static member ParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.DateTimeStyles -> DateTime
Public Shared Function ParseExact (s As ReadOnlySpan(Of Char), formats As String(), provider As IFormatProvider, Optional style As DateTimeStyles = System.Globalization.DateTimeStyles.None) As DateTime

パラメーター

s
ReadOnlySpan<Char>

変換する日付と時刻を表す文字を含むスパン。

formats
String[]

s の許可された形式の配列。

provider
IFormatProvider

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

style
DateTimeStyles

s で使用可能な書式を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、None です。

戻り値

formatsprovider、および style で指定された書式に従って表記された s に含まれる日時と等価のオブジェクト。

適用対象

ParseExact(String, String, IFormatProvider, DateTimeStyles)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

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

public:
 static DateTime ParseExact(System::String ^ s, System::String ^ format, IFormatProvider ^ provider, System::Globalization::DateTimeStyles style);
public static DateTime ParseExact (string s, string format, IFormatProvider provider, System.Globalization.DateTimeStyles style);
public static DateTime ParseExact (string s, string format, IFormatProvider? provider, System.Globalization.DateTimeStyles style);
static member ParseExact : string * string * IFormatProvider * System.Globalization.DateTimeStyles -> DateTime
Public Shared Function ParseExact (s As String, format As String, provider As IFormatProvider, style As DateTimeStyles) As DateTime

パラメーター

s
String

変換する日付と時刻を格納した文字列。

format
String

s の必要な形式を定義する形式指定子。 詳細については、「解説」を参照してください。

provider
IFormatProvider

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

style
DateTimeStyles

ss に指定できるスタイル要素、または s から DateTime 値への変換に関する追加情報を提供する列挙値のビットごとの組み合わせ。 通常指定する値は、None です。

戻り値

formatprovider、および style で指定された書式に従って表記された s に含まれる日時と等価のオブジェクト。

例外

s または formatnull です。

s または format が空の文字列です。

または

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

または

s の時間の部分と AM/PM 指定子が一致していません。

styleDateTimeStyles 値の正しくない組み合わせが含まれています。 たとえば、AssumeLocalAssumeUniversal の両方です。

ParseExact(String, String, IFormatProvider)メソッドの例を次に示します。 文字列 " 5/01/2009 8:30 AM" は、 によってformat先頭のスペースが許可されないため、 パラメーターが等しいDateTimeStyles.None場合stylesに正常に解析できないことに注意してください。 さらに、"5/01/2009 09:00" という文字列は、"MM/dd/yyyyhh:mm" で正常 format に解析できません。日付文字列は、必要に応じて format 月番号の前に 0 を付けないためです。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo enUS = new CultureInfo("en-US");
      string dateString;
      DateTime dateValue;

      // Parse date with no style flags.
      dateString = " 5/01/2009 8:30 AM";
      try {
         dateValue = DateTime.ParseExact(dateString, "g", enUS, DateTimeStyles.None);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }
      // Allow a leading space in the date string.
      try {
         dateValue = DateTime.ParseExact(dateString, "g", enUS, DateTimeStyles.AllowLeadingWhite);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }

      // Use custom formats with M and MM.
      dateString = "5/01/2009 09:00";
      try {
         dateValue = DateTime.ParseExact(dateString, "M/dd/yyyy hh:mm", enUS, DateTimeStyles.None);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }
      // Allow a leading space in the date string.
      try {
         dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, DateTimeStyles.None);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }

      // Parse a string with time zone information.
      dateString = "05/01/2009 01:30:42 PM -05:00";
      try {
         dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.None);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }
      // Allow a leading space in the date string.
      try {
         dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS,
                                     DateTimeStyles.AdjustToUniversal);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }

      // Parse a string representing UTC.
      dateString = "2008-06-11T16:11:20.0904778Z";
      try {
         dateValue = DateTime.ParseExact(dateString, "o", CultureInfo.InvariantCulture,
                                     DateTimeStyles.None);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }
      try {
         dateValue = DateTime.ParseExact(dateString, "o", CultureInfo.InvariantCulture,
                                     DateTimeStyles.RoundtripKind);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }
   }
}
// The example displays the following output:
//    ' 5/01/2009 8:30 AM' is not in an acceptable format.
//    Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified).
//    Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified).
//    '5/01/2009 09:00' is not in an acceptable format.
//    Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local).
//    Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc).
//    Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local).
//    Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).
open System
open System.Globalization

[<EntryPoint>]
let main _ =
    let enUS = CultureInfo "en-US"

    // Parse date with no style flags.
    let dateString = " 5/01/2009 8:30 AM"
    try
        let dateValue = DateTime.ParseExact(dateString, "g", enUS, DateTimeStyles.None)
        printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
    with :? FormatException ->
        printfn $"'{dateString}' is not in an acceptable format."
    
    // Allow a leading space in the date string.
    try
        let dateValue = DateTime.ParseExact(dateString, "g", enUS, DateTimeStyles.AllowLeadingWhite)
        printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
    with :? FormatException ->
        printfn $"'{dateString}' is not in an acceptable format."

    // Use custom formats with M and MM.
    let dateString = "5/01/2009 09:00"
    try
        let dateValue = DateTime.ParseExact(dateString, "M/dd/yyyy hh:mm", enUS, DateTimeStyles.None)
        printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
    with :? FormatException ->
        printfn $"'{dateString}' is not in an acceptable format."

    // Allow a leading space in the date string.
    try
        let dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, DateTimeStyles.None)
        printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
    with :? FormatException ->
        printfn $"'{dateString}' is not in an acceptable format."

    // Parse a string with time zone information.
    let dateString = "05/01/2009 01:30:42 PM -05:00"
    try
        let dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.None)
        printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
    with :? FormatException ->
        printfn $"'{dateString}' is not in an acceptable format."

    // Allow a leading space in the date string.
    try
        let dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.AdjustToUniversal)
        printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
    with :? FormatException ->
        printfn $"'{dateString}' is not in an acceptable format."

    // Parse a string representing UTC.
    let dateString = "2008-06-11T16:11:20.0904778Z"
    try
        let dateValue = DateTime.ParseExact(dateString, "o", CultureInfo.InvariantCulture, DateTimeStyles.None)
        printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
    with :? FormatException ->
        printfn $"'{dateString}' is not in an acceptable format."

    try
        let dateValue = DateTime.ParseExact(dateString, "o", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)
        printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
    with :? FormatException ->
        printfn $"'{dateString}' is not in an acceptable format."


// The example displays the following output:
//    ' 5/01/2009 8:30 AM' is not in an acceptable format.
//    Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified).
//    Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified).
//    '5/01/2009 09:00' is not in an acceptable format.
//    Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local).
//    Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc).
//    Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local).
//    Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim enUS As New CultureInfo("en-US") 
      Dim dateString As String
      Dim dateValue As Date
      
      ' Parse date with no style flags.
      dateString = " 5/01/2009 8:30 AM"
      Try
         dateValue = Date.ParseExact(dateString, "g", enUS, DateTimeStyles.None)
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
                           dateValue.Kind)
      Catch e As FormatException
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
      End Try
      ' Allow a leading space in the date string.
      Try
         dateValue = Date.ParseExact(dateString, "g", enUS, DateTimeStyles.AllowLeadingWhite)
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
                           dateValue.Kind)
      Catch e As FormatException
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
      End Try
      
      ' Use custom formats with M and MM.
      dateString = "5/01/2009 09:00"
      Try
         dateValue = Date.ParseExact(dateString, "M/dd/yyyy hh:mm", enUS, DateTimeStyles.None)
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
                           dateValue.Kind)
      Catch e As FormatException
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
      End Try
      ' Allow a leading space in the date string.
      Try
         dateValue = Date.ParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, DateTimeStyles.None)
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
                           dateValue.Kind)
      Catch e As FormatException
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
      End Try

      ' Parse a string with time zone information.
      dateString = "05/01/2009 01:30:42 PM -05:00" 
      Try
         dateValue = Date.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.None)
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
                           dateValue.Kind)
      Catch e As FormatException
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
      End Try
      ' Allow a leading space in the date string.
      Try
         dateValue = Date.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, _
                                     DateTimeStyles.AdjustToUniversal)
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
                           dateValue.Kind)
      Catch e As FormatException
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
      End Try
           
      ' Parse a string representing UTC.
      dateString = "2008-06-11T16:11:20.0904778Z"
      Try
         dateValue = Date.ParseExact(dateString, "o", CultureInfo.InvariantCulture, _
                                     DateTimeStyles.None)
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
                           dateValue.Kind)
      Catch e As FormatException
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
      End Try
      Try
         dateValue = Date.ParseExact(dateString, "o", CultureInfo.InvariantCulture, _
                                     DateTimeStyles.RoundtripKind)
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
                           dateValue.Kind)
      Catch e As FormatException
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
      End Try
   End Sub
End Module
' The example displays the following output:
'    ' 5/01/2009 8:30 AM' is not in an acceptable format.
'    Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified).
'    Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified).
'    '5/01/2009 09:00' is not in an acceptable format.
'    Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local).
'    Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc).
'    Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local).
'    Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).

注釈

メソッドは DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) 、日付の文字列表現を解析します。これは、 パラメーターで定義された format 形式である必要があります。 また、 の日付と時刻の要素 s は、 で指定された format順序で表示される必要があります。 が パラメーターのformatパターンと一致しない場合、パラメーターによってstyle定義されたバリエーションがある場合s、メソッドは をFormatExceptionスローします。 これに対し、 メソッドは DateTime.Parse(String, IFormatProvider, DateTimeStyles) 、書式プロバイダー DateTimeFormatInfo の オブジェクトによって認識されるいずれかの形式で日付の文字列表現を解析します。 メソッドでは DateTime.Parse(String, IFormatProvider, DateTimeStyles) 、 の日付と時刻の s 要素を任意の順序で表示することもできます。

パラメーターにs時刻のみが含まれており、日付が含まれない場合、パラメーターに フラグが含まれないDateTimeStyles.NoCurrentDateDefault限りstyle、現在の日付が使用されます。この場合、既定の日付 (DateTime.Date.MinValue) が使用されます。 パラメーターに s 日付だけが含まれており、時刻がない場合は、午前 0 時 (00:00:00) が使用されます。 パラメーターは style 、先頭、内側、または末尾の s 空白文字をパラメーターに含めることができるかどうかを決定します。

タイム ゾーン情報が含まれない場合s、返されるDateTimeオブジェクトの プロパティは ですDateTimeKind.UnspecifiedKind。 この動作は、プロパティが の値を返す フラグをDateTimeStyles.AssumeLocal使用するか、 プロパティが DateTimeKind.Localの値KindKindDateTimeKind.Utcを返す DateTimeDateTimeStyles.AdjustToUniversal フラグを使用DateTimeStyles.AssumeUniversalして変更できます。DateTime タイム ゾーン情報が含まれている場合s、時刻は必要に応じて現地時刻に変換されKind、返されるオブジェクトの プロパティは にDateTimeKind.Local設定されますDateTime。 この動作を変更するには、 フラグを DateTimeStyles.RoundtripKind 使用して協定世界時 (UTC) を現地時刻に変換せず、 プロパティを KindDateTimeKind.Utc設定します。

パラメーターは format 、パラメーターの必須パターンを s 定義します。 カスタムの日付と時刻の書式指定文字列テーブルの 1 つ以上のカスタム書式指定子、または標準 の日付と時刻の書式指定文字列 テーブルから定義済みのパターンを識別する 1 つの標準 書式 指定子で構成できます。

カスタム書式パターンで日付または時刻の区切り記号を使用しない場合は、 パラメーターのインバリアント カルチャ provider と、各カスタム書式指定子の最も広い形式を使用します。 たとえば、パターンで時間を指定する場合は、狭い形式ではなく、より広い形式 "HH" を指定します。

注意

解析操作を s 成功させるために 1 つの形式に準拠することを要求するのではなく、 メソッドを呼び出して、複数の DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 許可される形式を指定できます。 これにより、解析操作が成功する可能性が高くなります。

パラメーターにはstyles、によって定義formatされていない空白を表示sできるかどうかを決定し、解析操作のDateTimeStyles正確な動作を制御する列挙体の 1 つ以上のメンバーが含まれます。 次の表は、列挙体の各メンバーが DateTimeStyles メソッドの ParseExact(String, String, IFormatProvider, DateTimeStyles) 操作にどのように影響するかを示しています。

DateTimeStyles メンバー 説明
AdjustToUniversal sを解析し、必要に応じて UTC に変換します。 タイム ゾーン オフセットが含まれている場合s、またはタイム ゾーン情報が含まれないが フラグがstyles含まれているDateTimeStyles.AssumeLocal場合s、メソッドは文字列を解析し、 を呼び出ToUniversalTimeして戻りDateTime値を UTC に変換し、 プロパティを KindDateTimeKind.Utc設定します。 が UTC を表していることを示す場合s、またはタイム ゾーン情報が含まれていないが フラグがstyles含まれているDateTimeStyles.AssumeUniversal場合s、メソッドは文字列を解析し、返されたDateTime値に対してタイム ゾーン変換を実行せず、 プロパティを KindDateTimeKind.Utc設定します。 それ以外の場合、フラグは無効です。
AllowInnerWhite によって format 定義されていない空白を個々の日付または時刻要素の間に表示できることを指定します。
AllowLeadingWhite によって format 定義されていない空白を の先頭 sに表示できることを指定します。
AllowTrailingWhite によって format 定義されていない空白を の s末尾に表示できることを指定します。
AllowWhiteSpaces によってformat定義されていない先頭、内側、末尾の空白を含めることができます。s
AssumeLocal タイム ゾーン情報がない場合 s は、現地時刻を表すと見なされることを指定します。 フラグがDateTimeStyles.AdjustToUniversal存在しない限り、Kind戻り値の プロパティは にDateTimeKind.Local設定されますDateTime
AssumeUniversal タイム ゾーン情報がない場合 s は、UTC を表すと見なされることを指定します。 フラグが DateTimeStyles.AdjustToUniversal 存在しない限り、 メソッドは返された DateTime 値を UTC から現地時刻に変換し、そのプロパティを KindDateTimeKind.Local設定します。
NoCurrentDateDefault 日付情報のない時刻が含まれている場合 s 、戻り値の日付は に DateTime.MinValue.Date設定されます。
None パラメーターは s 既定値を使用して解析されます。 に存在する空白以外の format 空白は使用できません。 日付コンポーネントがない場合 s 、返される値の日付は 1/1/0001 に設定されます DateTime 。 タイム ゾーン情報が含まれない場合sKind返されるオブジェクトの プロパティは にDateTimeKind.Unspecified設定されますDateTime。 にタイム ゾーン情報が存在sする場合、時刻はローカル時刻に変換され、Kind返されるオブジェクトの プロパティは にDateTimeKind.Local設定されますDateTime
RoundtripKind タイム ゾーン情報を含む文字列の場合、プロパティが に設定された値の日付と時刻KindへのDateTimeDateTimeKind.Local変換を回避しようとします。 このフラグは、主に UTC 時刻から現地時刻への変換を防ぎます。

で使用されるs特定の日付と時刻の記号と文字列 (特定の言語の曜日の名前など) は、 パラメーターによってprovider定義されます。これは、 が標準書式指定子文字列である場合formats正確な形式です。 providerパラメーターには、次のいずれかを指定できます。

nullの場合providerは、CultureInfo現在のカルチャに対応する オブジェクトが使用されます。

注意 (呼び出し元)

.NET Framework 4 では、ParseExact解析される文字列に時間コンポーネントと、一致しない AM/PM 指定子が含まれている場合、メソッドは をスローFormatExceptionします。 .NET Framework 3.5 以前のバージョンでは、AM/PM 指定子は無視されます。

こちらもご覧ください

適用対象

ParseExact(String, String[], IFormatProvider, DateTimeStyles)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

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

public:
 static DateTime ParseExact(System::String ^ s, cli::array <System::String ^> ^ formats, IFormatProvider ^ provider, System::Globalization::DateTimeStyles style);
public static DateTime ParseExact (string s, string[] formats, IFormatProvider provider, System.Globalization.DateTimeStyles style);
public static DateTime ParseExact (string s, string[] formats, IFormatProvider? provider, System.Globalization.DateTimeStyles style);
static member ParseExact : string * string[] * IFormatProvider * System.Globalization.DateTimeStyles -> DateTime
Public Shared Function ParseExact (s As String, formats As String(), provider As IFormatProvider, style As DateTimeStyles) As DateTime

パラメーター

s
String

変換する日時を含む文字列。

formats
String[]

s の許可された形式の配列。 詳細については、「解説」を参照してください。

provider
IFormatProvider

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

style
DateTimeStyles

s で使用可能な書式を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、None です。

戻り値

formatsprovider、および style で指定された書式に従って表記された s に含まれる日時と等価のオブジェクト。

例外

s または formatsnull です。

s が空の文字列です。

または

formats の要素は空の文字列です。

または

s には、 formatsのどの要素に対応する日時も含まれていません。

または

s の時間の部分と AM/PM 指定子が一致していません。

styleDateTimeStyles 値の正しくない組み合わせが含まれています。 たとえば、AssumeLocalAssumeUniversal の両方です。

次の例では、 メソッドを DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 使用して、さまざまな形式の文字列を正常に解析できるようにします。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
                         "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
                         "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
                         "M/d/yyyy h:mm", "M/d/yyyy h:mm",
                         "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
                         "MM/d/yyyy HH:mm:ss.ffffff" };
      string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM",
                              "5/1/2009 6:32:00", "05/01/2009 06:32",
                              "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00",
                              "08/28/2015 16:17:39.125", "08/28/2015 16:17:39.125000" };
      DateTime dateValue;

      foreach (string dateString in dateStrings)
      {
         try {
            dateValue = DateTime.ParseExact(dateString, formats,
                                            new CultureInfo("en-US"),
                                            DateTimeStyles.None);
            Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
         }
         catch (FormatException) {
            Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
         }
      }
   }
}
// The example displays the following output:
//       Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.
//       Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.
//       Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.
//       Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.
//       Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.
//       Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.
//       Unable to convert '08/28/2015 16:17:39.125' to a date.
//       Converted '08/28/2015 16:17:39.125000' to 8/28/2015 4:17:39 PM.
open System
open System.Globalization

let formats = 
    [| "M/d/yyyy h:mm:ss tt"; "M/d/yyyy h:mm tt"
       "MM/dd/yyyy hh:mm:ss"; "M/d/yyyy h:mm:ss"
       "M/d/yyyy hh:mm tt"; "M/d/yyyy hh tt"
       "M/d/yyyy h:mm"; "M/d/yyyy h:mm"
       "MM/dd/yyyy hh:mm"; "M/dd/yyyy hh:mm"
       "MM/d/yyyy HH:mm:ss.ffffff" |]

let dateStrings = 
    [ "5/1/2009 6:32 PM"; "05/01/2009 6:32:05 PM"
      "5/1/2009 6:32:00"; "05/01/2009 06:32"
      "05/01/2009 06:32:00 PM"; "05/01/2009 06:32:00"
      "08/28/2015 16:17:39.125"; "08/28/2015 16:17:39.125000" ]

for dateString in dateStrings do
    try
        let dateValue = DateTime.ParseExact(dateString, formats, CultureInfo "en-US", DateTimeStyles.None)
        printfn $"Converted '{dateString}' to {dateValue}."
    with :? FormatException ->
        printfn $"Unable to convert '{dateString}' to a date."

// The example displays the following output:
//       Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.
//       Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.
//       Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.
//       Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.
//       Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.
//       Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.
//       Unable to convert '08/28/2015 16:17:39.125' to a date.
//       Converted '08/28/2015 16:17:39.125000' to 8/28/2015 4:17:39 PM.
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim formats() As String = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", _
                                 "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", _
                                 "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", _
                                 "M/d/yyyy h:mm", "M/d/yyyy h:mm", _
                                 "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
                                 "MM/d/yyyy HH:mm:ss.ffffff" }
      Dim dateStrings() As String = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM", _
                                     "5/1/2009 6:32:00", "05/01/2009 06:32", _
                                     "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00",
                                     "08/28/2015 16:17:39.125", "08/28/2015 16:17:39.125000" }

      Dim dateValue As DateTime
      
      For Each dateString As String In dateStrings
         Try
            dateValue = DateTime.ParseExact(dateString, formats, _
                                            New CultureInfo("en-US"), _
                                            DateTimeStyles.None)
            Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue)
         Catch e As FormatException
            Console.WriteLine("Unable to convert '{0}' to a date.", dateString)
         End Try                                               
      Next
   End Sub
End Module
' The example displays the following output:
'       Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.
'       Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.
'       Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.
'       Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.
'       Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.
'       Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.
'       Unable to convert '08/28/2015 16:17:39.125' to a date.
'       Converted '08/28/2015 16:17:39.125000' to 8/28/2015 4:17:39 PM.

注釈

メソッドは DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 、 パラメーターに割り当てられたパターンのいずれかに一致する日付の文字列表現を formats 解析します。 文字列 s がパラメーターによって styles 定義されたバリエーションのいずれかとこれらのパターンのいずれとも一致しない場合、 メソッドは を FormatExceptionスローします。 1 つの書式設定パターンではなく、複数の書式設定パターンと比較 s する以外に、このオーバーロードは メソッドと同じように DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) 動作します。

パラメーターには s 、解析する日付と時刻が含まれます。 パラメーターにs時刻のみが含まれており、日付が含まれない場合、パラメーターに フラグが含まれないDateTimeStyles.NoCurrentDateDefault限りstyle、現在の日付が使用されます。この場合、既定の日付 (DateTime.Date.MinValue) が使用されます。 パラメーターに s 日付だけが含まれており、時刻がない場合は、午前 0 時 (00:00:00) が使用されます。 パラメーターはstyle、 の書式指定文字列の 1 つで許可される文字以外の先頭、内側、または末尾の空白文字をパラメーターにformats含めることができるかどうかをs決定します。

タイム ゾーン情報が含まれない場合s、返されるDateTimeオブジェクトの プロパティは ですDateTimeKind.UnspecifiedKind。 この動作は、プロパティが の値を返す フラグをDateTimeStyles.AssumeLocal使用するか、 プロパティが DateTimeKind.Localの値KindKindDateTimeKind.Utcを返す DateTimeDateTimeStyles.AdjustToUniversal フラグを使用DateTimeStyles.AssumeUniversalして変更できます。DateTime タイム ゾーン情報が含まれている場合s、時刻は必要に応じて現地時刻に変換されKind、返されるオブジェクトの プロパティは にDateTimeKind.Local設定されますDateTime。 この動作を変更するには、 フラグを DateTimeStyles.RoundtripKind 使用して協定世界時 (UTC) を現地時刻に変換せず、 プロパティを KindDateTimeKind.Utc設定します。

パラメーターには formats パターンの配列が含まれています。そのうちの s 1 つは、解析操作が成功する場合に正確に一致する必要があります。 パラメーターの formats パターンは、カスタム日付と時刻書式指定文字列テーブルの 1 つ以上のカスタム書式指定子、または標準 の日付と時刻の書式指定文字列 テーブルから定義済みのパターンを識別する 1 つの標準 書式 指定子で構成されます。

カスタム書式パターンで日付または時刻の区切り記号を使用しない場合は、 パラメーターのインバリアント カルチャ provider と、各カスタム書式指定子の最も広い形式を使用します。 たとえば、パターンで時間を指定する場合は、狭い形式ではなく、より広い形式 "HH" を指定します。

パラメーターにはstyles、によって定義formatされていない空白を表示sできるかどうかを決定し、解析操作のDateTimeStyles正確な動作を制御する列挙体の 1 つ以上のメンバーが含まれます。 次の表は、列挙体の各メンバーが DateTimeStyles メソッドの ParseExact(String, String, IFormatProvider, DateTimeStyles) 操作にどのように影響するかを示しています。

DateTimeStyles メンバー 説明
AdjustToUniversal sを解析し、必要に応じて UTC に変換します。 タイム ゾーン オフセットが含まれている場合s、またはタイム ゾーン情報が含まれないが フラグがstyles含まれているDateTimeStyles.AssumeLocal場合s、メソッドは文字列を解析し、 を呼び出ToUniversalTimeして戻りDateTime値を UTC に変換し、 プロパティを KindDateTimeKind.Utc設定します。 が UTC を表していることを示す場合s、またはタイム ゾーン情報が含まれていないが フラグがstyles含まれているDateTimeStyles.AssumeUniversal場合s、メソッドは文字列を解析し、返されたDateTime値に対してタイム ゾーン変換を実行せず、 プロパティを KindDateTimeKind.Utc設定します。 それ以外の場合、フラグは無効です。
AllowInnerWhite によって format 定義されていない空白を個々の日付または時刻要素の間に表示できることを指定します。
AllowLeadingWhite によって format 定義されていない空白を の先頭 sに表示できることを指定します。
AllowTrailingWhite によって format 定義されていない空白を の s末尾に表示できることを指定します。
AllowWhiteSpaces によってformat定義されていない先頭、内側、末尾の空白を含めることができます。s
AssumeLocal タイム ゾーン情報がない場合 s は、現地時刻を表すと見なされることを指定します。 フラグがDateTimeStyles.AdjustToUniversal存在しない限り、Kind戻り値の プロパティは にDateTimeKind.Local設定されますDateTime
AssumeUniversal タイム ゾーン情報がない場合 s は、UTC を表すと見なされることを指定します。 フラグが DateTimeStyles.AdjustToUniversal 存在しない限り、 メソッドは返された DateTime 値を UTC から現地時刻に変換し、そのプロパティを KindDateTimeKind.Local設定します。
NoCurrentDateDefault 日付情報のない時刻が含まれている場合 s 、戻り値の日付は に DateTime.MinValue.Date設定されます。
None パラメーターは s 既定値を使用して解析されます。 に存在する空白以外の format 空白は使用できません。 日付コンポーネントがない場合 s 、戻り値の日付は 1/1/0001 に設定されます DateTime 。 タイム ゾーン情報が含まれない場合sKind返されるオブジェクトの プロパティは にDateTimeKind.Unspecified設定されますDateTime。 にタイム ゾーン情報が存在するs場合、時刻はローカル時刻に変換され、Kind返されるオブジェクトの プロパティは にDateTimeKind.Local設定されますDateTime
RoundtripKind タイム ゾーン情報を含む文字列の場合、プロパティが に設定DateTimeKind.Localされた日付と時刻Kindへの変換を防ぎます。 このフラグは、主に UTC 時刻からローカル時刻への変換を防ぎます。

で使用されるs特定の日付と時刻の記号と文字列 (特定の言語の曜日の名前など) は、 パラメーターによってprovider定義されます。これは、 が標準書式指定子文字列である場合formats正確な形式と同様です。 providerパラメーターには、次のいずれかを指定できます。

nullの場合provider、現在のCultureInfoカルチャに対応する オブジェクトが使用されます。

注意 (呼び出し元)

.NET Framework 4 では、ParseExact解析される文字列に時間コンポーネントと、一致しない AM/PM 指定子が含まれている場合、メソッドは をスローFormatExceptionします。 .NET Framework 3.5 以前のバージョンでは、AM/PM 指定子は無視されます。

こちらもご覧ください

適用対象