DateTime.ParseExact メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定した文字列形式の日付と時刻を等価の 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 つと完全に一致する必要があります。それ以外の場合は、例外がスローされます。 |
注釈
重要
和暦の時代 (年号) は天皇の代に基づいているため、変更されることが予想されます。 たとえば、JapaneseCalendar と JapaneseLunisolarCalendar において、2019 年 5 月 1 日から令和時代が始まることになりました。 このような時代 (年号) の変更は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細およびアプリケーションが影響を受けるかどうかを判断する方法については、「 .net の日本語カレンダーでの新しい時代 (年号) の処理」を参照してください。 Windows システムでアプリケーションをテストして、時代 (年号) の変化に対応できるようにする方法については、「日本語時代 (年号) の変更に備えてアプリケーションを準備する」を参照してください。 複数の時代 (年号) を含む暦をサポートする .NET の機能について、および複数の時代 (年号) をサポートする予定表を操作する場合のベストプラクティスについては、「時代と作業
ParseExact(String, String, IFormatProvider)
指定した書式とカルチャ固有の書式情報を使用して、指定した日付と時刻の文字列形式を等価の 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
または format
が null
です。
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
形式である必要があります。 また、日付と時刻の文字列表現の要素は<Time>、指定されたformat
順序で表示されs
、許可されているformat
空白以外の空白を持たない必要<Date>があります。 time 要素のない日付を定義し、解析操作が成功した場合 format
、結果の DateTime 値の時刻は午前 0 時 (00:00:00) になります。 日付要素を持たない時刻を定義し、解析操作が成功した場合 format
、結果 DateTime の値の日付 DateTime.Now.Date
は .
特定のタイム ゾーン内の時刻を表していない場合 s
、解析操作が成功すると、 Kind 返される DateTime 値のプロパティは DateTimeKind.Unspecified. 特定のタイム ゾーンの時刻を表しformat
、タイム ゾーン情報が存在できるようにする場合 s
(たとえば、"o"、format
"r"、または "u" 標準書式指定子と等しい場合、または "z"、"zz"、または "zzz" カスタム書式指定子が含まれている場合)Kind、返されるDateTime値のプロパティはDateTimeKind.Localです。
format
パラメーターは、1 つの標準書式指定子、または必要な書式を定義する 1 つ以上のs
カスタム書式指定子を含む文字列です。 有効な書式指定コードの詳細については、「 標準の日時書式指定文字列 」または 「カスタム日時書式指定文字列」を参照してください。
注意
日付または時刻の区切り記号 ("yyyyMMddHHmm" など) を含まないカスタム書式パターンの場合 format
は、パラメーターのインバリアント カルチャ provider
と、各カスタム書式指定子の最も広い形式を使用します。 たとえば、書式パターンで時間を指定する場合は、狭い形式の代わりに、より広い形式 "HH" を指定します。"H" です。
使用される特定の日付と時刻の記号と文字列 (特定の言語の曜日の名前など) s
は、標準書式指定子文字列である場合format
のs
正確な形式と同様に、パラメーターによってprovider
定義されます。 provider
パラメーターには、次のいずれかを指定できます。
CultureInfo解釈
s
に使用するカルチャを表すオブジェクト。 そのDateTimeFormatプロパティによって返されるオブジェクトはDateTimeFormatInfo、シンボルと書式設定をs
定義します。DateTimeFormatInfo日付と時刻のデータの形式を定義するオブジェクト。
書式設定情報をGetFormat提供するオブジェクトまたはオブジェクトをCultureInfo返すメソッドをDateTimeFormatInfo持つカスタムIFormatProvider実装。
ある場合 provider
は null
、現在の CultureInfo カルチャに対応するオブジェクトが使用されます。
注意 (呼び出し元)
.NET Framework 4 では、ParseExact解析する文字列に時間コンポーネントと、一致しない AM/PM 指定子が含まれている場合、メソッドは a FormatException をスローします。 .NET Framework 3.5 以前のバージョンでは、AM/PM 指定子は無視されます。
こちらもご覧ください
- TryParseExact
- CultureInfo
- DateTimeFormatInfo
- .NET Framework における日付と時刻文字列の解析の解析
- 標準の日時書式指定文字列
- カスタム日時書式指定文字列
適用対象
ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)
指定した書式、カルチャ固有の書式情報、スタイルを使用して、指定した日付と時刻のスパン表現を、それと等価な 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
s
、s
に指定できるスタイル要素、または s
から DateTime 値への変換に関する追加情報を提供する列挙値のビットごとの組み合わせ。 通常指定する値は、None です。
戻り値
format
、provider
、および style
で指定された書式に従って表記された s
に含まれる日時と等価のオブジェクト。
適用対象
ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)
指定した書式の配列、カルチャ固有の書式情報、スタイルを使用して、指定した日付と時刻のスパン表現を、それと等価な 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 です。
戻り値
formats
、provider
、および style
で指定された書式に従って表記された s
に含まれる日時と等価のオブジェクト。
適用対象
ParseExact(String, String, IFormatProvider, DateTimeStyles)
指定した書式、カルチャ固有の書式情報、およびスタイルを使用して、指定した日付と時刻の文字列形式を等価の 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
s
、s
に指定できるスタイル要素、または s
から DateTime 値への変換に関する追加情報を提供する列挙値のビットごとの組み合わせ。 通常指定する値は、None です。
戻り値
format
、provider
、および style
で指定された書式に従って表記された s
に含まれる日時と等価のオブジェクト。
例外
s
または format
が null
です。
s
または format
が空の文字列です。
または
s
に、format
で指定されているパターンに対応する日付と時刻が含まれていません。
または
s
の時間の部分と AM/PM 指定子が一致していません。
style
に DateTimeStyles 値の正しくない組み合わせが含まれています。 たとえば、AssumeLocal と AssumeUniversal の両方です。
例
ParseExact(String, String, IFormatProvider)メソッドの例を次に示します。 先頭のスペースが許可format
されていないため、パラメーターが等しいDateTimeStyles.None場合styles
、文字列 "5/01/2009 8:30 AM" は正常に解析できないことに注意してください。 さらに、文字列 "5/01/2009 09:00" は"MM/dd/yyyyh: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返すDateTimeフラグを使用するか、プロパティが DateTimeKind.Local の値DateTimeKind.UtcKindをDateTimeStyles.AssumeUniversal返す DateTime and DateTimeStyles.AdjustToUniversal フラグを使用して変更できます。Kind タイム ゾーン情報が含まれている場合 s
、時刻は必要に応じてローカル時刻に変換され Kind 、返される DateTime オブジェクトのプロパティは DateTimeKind.Local. この動作は、フラグをDateTimeStyles.RoundtripKind使用して協定世界時 (UTC) を現地時刻に変換せず、プロパティDateTimeKind.Utcを Kind .
パラメーターは format
、パラメーターの必要なパターンを s
定義します。 カスタム日時書式指定文字列テーブルの 1 つ以上のカスタム書式指定子、または標準 の日付と時刻書式指定文字列 テーブルから定義済みのパターンを識別する 1 つの標準 書式指定子 で構成できます。
カスタム書式パターンで日付または時刻の区切り記号を使用しない場合は、パラメーターのインバリアント カルチャ provider
と、各カスタム書式指定子の最も広い形式を使用します。 たとえば、パターンで時間を指定する場合は、より狭い形式の代わりに、より広い形式 "HH" を指定します。"H"
注意
解析操作を 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 、メソッドは文字列を解析し、返されたDateTime値を UTC に変換する呼び出しを呼び出ToUniversalTimeし、プロパティを Kind DateTimeKind.Utc. UTC を表していることを示す場合s 、またはタイム ゾーン情報を含まないがフラグがstyles 含まれているDateTimeStyles.AssumeUniversal場合s 、メソッドは文字列を解析し、返されたDateTime値に対してタイム ゾーン変換を実行せず、プロパティを Kind DateTimeKind.Utc. それ以外の場合は、フラグは無効です。 |
AllowInnerWhite | 定義されていない format 空白を個々の日付または時刻要素の間に表示できることを指定します。 |
AllowLeadingWhite | によって定義 format されていない空白を先頭 s に表示できることを指定します。 |
AllowTrailingWhite | 定義されていない format 空白を末尾 s に表示できることを指定します。 |
AllowWhiteSpaces | で定義format されていない先頭、内側、末尾の空白を含めることができますをs 指定します。 |
AssumeLocal | タイム ゾーン情報がない場合 s は、ローカル時刻を表すと見なされることを指定します。 フラグが DateTimeStyles.AdjustToUniversal 存在しない限り、 Kind 戻り DateTime 値のプロパティは DateTimeKind.Local. |
AssumeUniversal | タイム ゾーン情報がない場合 s は、UTC を表すと見なされることを指定します。 フラグが DateTimeStyles.AdjustToUniversal 存在しない限り、メソッドは返された DateTime 値を UTC からローカル時刻に変換し、そのプロパティを Kind DateTimeKind.Local. |
NoCurrentDateDefault | 日付情報のない時刻が含まれている場合 s 、戻り値の日付は DateTime.MinValue.Date . |
None | パラメーターは s 既定値を使用して解析されます。 空白以外の format 空白は使用できません。 日付コンポーネントがない場合 s 、戻り DateTime 値の日付は 1/1/0001 に設定されます。 タイム ゾーン情報が含まれている場合s 、返されるDateTimeオブジェクトのプロパティは DateTimeKind.UnspecifiedKind . タイム ゾーン情報が存在する s 場合、時刻はローカル時刻に変換され、 Kind 返される DateTime オブジェクトのプロパティは DateTimeKind.Local. |
RoundtripKind | タイム ゾーン情報を含む文字列の場合は、プロパティが > に設定された値の日付と時刻KindへのDateTimeDateTimeKind.Local変換を回避します。 このフラグは、主に UTC 時刻からローカル時刻への変換を禁止します。 |
使用される特定の日付と時刻の記号と文字列 (特定の言語の曜日の名前など) s
は、標準書式指定子文字列である場合format
のs
正確な形式と同様に、パラメーターによってprovider
定義されます。 provider
パラメーターには、次のいずれかを指定できます。
CultureInfo解釈
s
に使用するカルチャを表すオブジェクト。 そのDateTimeFormatプロパティによって返されるオブジェクトはDateTimeFormatInfo、シンボルと書式設定をs
定義します。DateTimeFormatInfo日付と時刻のデータの形式を定義するオブジェクト。
書式設定情報をGetFormat提供するオブジェクトまたはオブジェクトをCultureInfo返すメソッドをDateTimeFormatInfo持つカスタムIFormatProvider実装。
ある場合 provider
は null
、現在の CultureInfo カルチャに対応するオブジェクトが使用されます。
注意 (呼び出し元)
.NET Framework 4 では、ParseExact解析する文字列に時間コンポーネントと、一致しない AM/PM 指定子が含まれている場合、メソッドは a FormatException をスローします。 .NET Framework 3.5 以前のバージョンでは、AM/PM 指定子は無視されます。
こちらもご覧ください
- TryParseExact
- CultureInfo
- DateTimeFormatInfo
- .NET Framework における日付と時刻文字列の解析の解析
- 標準の日時書式指定文字列
- カスタム日時書式指定文字列
適用対象
ParseExact(String, String[], IFormatProvider, DateTimeStyles)
指定した書式の配列、カルチャ固有の書式情報、およびスタイルを使用して、指定した日付と時刻の文字列形式を等価の 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 です。
戻り値
formats
、provider
、および style
で指定された書式に従って表記された s
に含まれる日時と等価のオブジェクト。
例外
s
または formats
が null
です。
s
が空の文字列です。
または
formats
の要素は空の文字列です。
または
s
には、 formats
のどの要素に対応する日時も含まれていません。
または
s
の時間の部分と AM/PM 指定子が一致していません。
style
に DateTimeStyles 値の正しくない組み合わせが含まれています。 たとえば、AssumeLocal と AssumeUniversal の両方です。
例
次の例では、メソッドを 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
、パラメーターに先頭、内側、または末尾の空白文字を含めることができるかどうか s
も決定します。これは、次のいずれかの書式指定文字列 formats
で許可されている文字以外の空白文字です。
タイム ゾーン情報が含まれている場合s
、返されるDateTimeオブジェクトのプロパティは DateTimeKind.UnspecifiedKind . この動作は、プロパティを持つ値をDateTimeStyles.AssumeLocal返すDateTimeフラグを使用するか、プロパティが DateTimeKind.Local の値DateTimeKind.UtcKindを返す DateTime and DateTimeStyles.AdjustToUniversal フラグを使用DateTimeStyles.AssumeUniversalして変更できます。Kind タイム ゾーン情報が含まれている場合 s
、時刻は必要に応じてローカル時刻に変換され Kind 、返される DateTime オブジェクトのプロパティは DateTimeKind.Local. この動作は、フラグをDateTimeStyles.RoundtripKind使用して協定世界時 (UTC) を現地時刻に変換せず、プロパティDateTimeKind.Utcを Kind .
パラメーターには formats
パターンの配列が含まれています。そのうちの s
1 つは、解析操作が成功する場合に正確に一致する必要があります。 パラメーターの formats
パターンは、 カスタム日付と時刻書式指定文字列 テーブルの 1 つ以上のカスタム書式指定子、または 標準の日付と時刻 書式指定文字列テーブルから定義済みのパターンを識別する 1 つの標準書式指定子で構成されます。
カスタム書式パターンで日付または時刻の区切り記号を使用しない場合は、パラメーターのインバリアント カルチャ provider
と、各カスタム書式指定子の最も広い形式を使用します。 たとえば、パターンで時間を指定する場合は、より狭い形式である "H" ではなく、より広い形式 "HH" を指定します。
パラメーターにはstyles
、定義されていないformat
空白を表示できるかどうか、およびどこに空白を含s
めることができるかを決定し、解析操作の正確な動作を制御する列挙体の 1 つ以上のメンバーDateTimeStylesが含まれます。 次の表では、列挙体の各メンバーが DateTimeStyles メソッドの操作 ParseExact(String, String, IFormatProvider, DateTimeStyles) にどのように影響するかを説明します。
DateTimeStyles メンバー | 説明 |
---|---|
AdjustToUniversal | s 解析し、必要に応じて UTC に変換します。 タイム ゾーン オフセットが含まれている場合s 、またはタイム ゾーン情報が含まれているがフラグがstyles 含まれているDateTimeStyles.AssumeLocal場合s 、メソッドは文字列を解析し、戻りDateTime値を UTC に変換する呼び出しを行ToUniversalTimeい、プロパティを Kind DateTimeKind.Utc. UTC を表していることを示す場合s 、またはタイム ゾーン情報を含まないがフラグがstyles 含まれているDateTimeStyles.AssumeUniversal場合s 、メソッドは文字列を解析し、戻りDateTime値に対してタイム ゾーン変換を実行せず、プロパティを Kind にDateTimeKind.Utc設定します。 それ以外の場合は、フラグは無効です。 |
AllowInnerWhite | 定義されていない format 空白を個々の日付要素または時刻要素の間に表示できることを指定します。 |
AllowLeadingWhite | 定義されていない format 空白を先頭に s 表示できることを指定します。 |
AllowTrailingWhite | 定義されていない format 空白を末尾 s に表示できることを指定します。 |
AllowWhiteSpaces | で定義format されていない先頭、内側、末尾の空白を含めることができますをs 指定します。 |
AssumeLocal | タイム ゾーン情報がない場合 s は、ローカル時刻を表すと見なされることを指定します。 フラグが DateTimeStyles.AdjustToUniversal 存在しない限り、 Kind 戻り DateTime 値のプロパティは DateTimeKind.Local. |
AssumeUniversal | タイム ゾーン情報がない場合 s は、UTC を表すと見なされることを指定します。 フラグが DateTimeStyles.AdjustToUniversal 存在しない限り、メソッドは返された DateTime 値を UTC から現地時刻に変換し、その Kind プロパティを DateTimeKind.Local. |
NoCurrentDateDefault | 日付情報のない時刻が含まれている場合 s 、戻り値の日付は DateTime.MinValue.Date . |
None | パラメーターは s 既定値を使用して解析されます。 空白以外の format 空白は使用できません。 日付コンポーネントがない場合 s 、戻り DateTime 値の日付は 1/1/0001 に設定されます。 タイム ゾーン情報が含まれている場合 s は、 Kind 返される DateTime オブジェクトのプロパティがに設定 DateTimeKind.Unspecifiedされます。 タイム ゾーン情報が存在する s 場合、時刻はローカル時刻に変換され、 Kind 返される DateTime オブジェクトのプロパティは DateTimeKind.Local. |
RoundtripKind | タイム ゾーン情報を含む文字列の場合は、そのプロパティが > に設定DateTimeKind.Localされた日付と時刻Kindへの変換を回避しようとします。 このフラグは、主に UTC 時刻からローカル時刻への変換を禁止します。 |
使用されるs
特定の日付と時刻の記号と文字列 (特定の言語の曜日の名前など) は、標準書式指定子文字列である場合format
のs
正確な形式と同様に、パラメーターによってprovider
定義されます。 provider
パラメーターには、次のいずれかを指定できます。
CultureInfo解釈
s
に使用されるカルチャを表すオブジェクト。 プロパティDateTimeFormatによって返されるオブジェクトはDateTimeFormatInfo、シンボルと書式設定をs
定義します。DateTimeFormatInfo日付と時刻のデータの形式を定義するオブジェクト。
書式設定情報をGetFormat提供するオブジェクトまたはDateTimeFormatInfoオブジェクトをCultureInfo返すメソッドを持つカスタムIFormatProvider実装。
ある場合 provider
は null
、現在の CultureInfo カルチャに対応するオブジェクトが使用されます。
注意 (呼び出し元)
.NET Framework 4 では、ParseExact解析される文字列に時間コンポーネントと、一致しない AM/PM 指定子が含まれている場合、メソッドは a FormatException をスローします。 .NET Framework 3.5 以前のバージョンでは、AM/PM 指定子は無視されます。
こちらもご覧ください
- TryParseExact
- CultureInfo
- DateTimeFormatInfo
- .NET Framework における日付と時刻文字列の解析の解析
- 標準の日時書式指定文字列
- カスタム日時書式指定文字列