DateTime.TryParse メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定した文字列形式の日時を対応する DateTime 表現に変換し、変換に成功したかどうかを示す値を返します。
オーバーロード
TryParse(String, IFormatProvider, DateTimeStyles, DateTime) |
指定したカルチャ固有の書式情報と書式スタイルを使用して、指定した文字列形式の日付と時刻をそれと等価の DateTime に変換し、変換に成功したかどうかを示す値を返します。 |
TryParse(String, IFormatProvider, DateTime) |
文字列を値に解析しようとします。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTime) |
文字の範囲を解析して値を取得しようとします。 |
TryParse(ReadOnlySpan<Char>, DateTime) |
指定した日付と時刻の文字スパンを、それと等価な DateTime に変換し、変換が成功したかどうかを示す値を返します。 |
TryParse(String, DateTime) |
指定した文字列形式の日時を対応する DateTime 表現に変換し、変換に成功したかどうかを示す値を返します。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime) |
指定したカルチャ固有の書式情報と書式スタイルを使用して、日付と時刻のスパン表現を、それと等価な DateTime に変換し、変換が成功したかどうかを示す値を返します。 |
注釈
重要
和暦の時代 (年号) は天皇の代に基づいているため、変更されることが予想されます。 たとえば、JapaneseCalendar と JapaneseLunisolarCalendar において、2019 年 5 月 1 日から令和時代が始まることになりました。 このような時代 (年号) の変更は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細およびアプリケーションが影響を受けるかどうかを判断する方法については、「 .net の日本語カレンダーでの新しい時代 (年号) の処理」を参照してください。 Windows システムでアプリケーションをテストして、時代 (年号) の変化に対応できるようにする方法については、「日本語時代 (年号) の変更に備えてアプリケーションを準備する」を参照してください。 複数の時代 (年号) を含む暦をサポートする .NET の機能について、および複数の時代 (年号) をサポートする予定表を操作する場合のベストプラクティスについては、「時代と作業
TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
指定したカルチャ固有の書式情報と書式スタイルを使用して、指定した文字列形式の日付と時刻をそれと等価の DateTime に変換し、変換に成功したかどうかを示す値を返します。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTime % result);
public static bool TryParse (string s, IFormatProvider provider, System.Globalization.DateTimeStyles styles, out DateTime result);
public static bool TryParse (string? s, IFormatProvider? provider, System.Globalization.DateTimeStyles styles, out DateTime result);
static member TryParse : string * IFormatProvider * System.Globalization.DateTimeStyles * DateTime -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTime) As Boolean
パラメーター
- s
- String
変換する日付と時刻を格納した文字列。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- styles
- DateTimeStyles
現在のタイム ゾーンまたは現在の日付と関連させて、解析された日付をどのように解釈するかを定義する列挙値のビットごとの組み合わせ。 通常指定する値は、None です。
- result
- DateTime
このメソッドから制御が戻るときに、変換が成功した場合は日付と時刻に相当する値を格納し、変換に失敗した場合は DateTime.MinValue を格納DateTimes
します。 s
パラメーターが null
の場合、空の文字列 ("") の場合、または日付と時刻を表す有効な文字列形式が含まれていない場合は、変換に失敗します。 このパラメーターは初期化せずに渡されます。
戻り値
s
パラメーターが正常に変換された場合は true
。それ以外の場合は false
。
例外
styles
は有効な DateTimeStyles 値ではありません。
または
styles
に DateTimeStyles 値の正しくない組み合わせが含まれています (たとえば、AssumeLocal と AssumeUniversal の両方などです)。
provider
はニュートラル カルチャであり、解析操作で使用することはできません。
例
次の例は、このメソッドを DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime) 示しています。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string dateString;
CultureInfo culture;
DateTimeStyles styles;
DateTime dateResult;
// Parse a date and time with no styles.
dateString = "03/01/2009 10:00 AM";
culture = CultureInfo.CreateSpecificCulture("en-US");
styles = DateTimeStyles.None;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.",
dateString);
// Parse the same date and time with the AssumeLocal style.
styles = DateTimeStyles.AssumeLocal;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
// Parse a date and time that is assumed to be local.
// This time is five hours behind UTC. The local system's time zone is
// eight hours behind UTC.
dateString = "2009/03/01T10:00:00-5:00";
styles = DateTimeStyles.AssumeLocal;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
// Attempt to convert a string in improper ISO 8601 format.
dateString = "03/01/2009T10:00:00-5:00";
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
// Assume a date and time string formatted for the fr-FR culture is the local
// time and convert it to UTC.
dateString = "2008-03-01 10:00";
culture = CultureInfo.CreateSpecificCulture("fr-FR");
styles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
}
}
// The example displays the following output to the console:
// 03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Unspecified.
// 03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Local.
// 2009/03/01T10:00:00-5:00 converted to 3/1/2009 7:00:00 AM Local.
// Unable to convert 03/01/2009T10:00:00-5:00 to a date and time.
// 2008-03-01 10:00 converted to 3/1/2008 6:00:00 PM Utc.
open System
open System.Globalization
[<EntryPoint>]
let main _ =
// Parse a date and time with no styles.
let dateString = "03/01/2009 10:00 AM"
let culture = CultureInfo.CreateSpecificCulture "en-US"
let styles = DateTimeStyles.None
match DateTime.TryParse(dateString, culture, styles) with
| true, dateResult ->
printfn $"{dateString} converted to {dateResult} {dateResult.Kind}."
| _ ->
printfn $"Unable to convert {dateString} to a date and time."
// Parse the same date and time with the AssumeLocal style.
let styles = DateTimeStyles.AssumeLocal
match DateTime.TryParse(dateString, culture, styles) with
| true, dateResult ->
printfn $"{dateString} converted to {dateResult} {dateResult.Kind}."
| _ ->
printfn $"Unable to convert {dateString} to a date and time."
// Parse a date and time that is assumed to be local.
// This time is five hours behind UTC. The local system's time zone is
// eight hours behind UTC.
let dateString = "2009/03/01T10:00:00-5:00"
let styles = DateTimeStyles.AssumeLocal
match DateTime.TryParse(dateString, culture, styles) with
| true, dateResult ->
printfn $"{dateString} converted to {dateResult} {dateResult.Kind}."
| _ ->
printfn $"Unable to convert {dateString} to a date and time."
// Attempt to convert a string in improper ISO 8601 format.
let dateString = "03/01/2009T10:00:00-5:00"
match DateTime.TryParse(dateString, culture, styles) with
| true, dateResult ->
printfn $"{dateString} converted to {dateResult} {dateResult.Kind}."
| _ ->
printfn $"Unable to convert {dateString} to a date and time."
// Assume a date and time string formatted for the fr-FR culture is the local
// time and convert it to UTC.
let dateString = "2008-03-01 10:00"
let culture = CultureInfo.CreateSpecificCulture "fr-FR"
let styles = DateTimeStyles.AdjustToUniversal ||| DateTimeStyles.AssumeLocal
match DateTime.TryParse(dateString, culture, styles) with
| true, dateResult ->
printfn $"{dateString} converted to {dateResult} {dateResult.Kind}."
| _ ->
printfn $"Unable to convert {dateString} to a date and time."
0
// The example displays the following output to the console:
// 03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Unspecified.
// 03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Local.
// 2009/03/01T10:00:00-5:00 converted to 3/1/2009 7:00:00 AM Local.
// Unable to convert 03/01/2009T10:00:00-5:00 to a date and time.
// 2008-03-01 10:00 converted to 3/1/2008 6:00:00 PM Utc.
Imports System.Globalization
Public Module Example
Public Sub Main()
Dim dateString As String
Dim culture As CultureInfo
Dim styles As DateTimeStyles
Dim dateResult As DateTime
' Parse a date and time with no styles.
dateString = "03/01/2009 10:00 AM"
culture = CultureInfo.CreateSpecificCulture("en-US")
styles = DateTimeStyles.None
If DateTime.TryParse(dateString, culture, styles, dateResult) Then
Console.WriteLine("{0} converted to {1} {2}.", _
dateString, dateResult, dateResult.Kind)
Else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
End If
' Parse the same date and time with the AssumeLocal style.
styles = DateTimeStyles.AssumeLocal
If DateTime.TryParse(dateString, culture, styles, dateResult)
Console.WriteLine("{0} converted to {1} {2}.", _
dateString, dateResult, dateResult.Kind)
Else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
End If
' Parse a date and time that is assumed to be local.
' This time is five hours behind UTC. The local system's time zone is
' eight hours behind UTC.
dateString = "2009/03/01T10:00:00-5:00"
styles = DateTimeStyles.AssumeLocal
If DateTime.TryParse(dateString, culture, styles, dateResult)
Console.WriteLine("{0} converted to {1} {2}.", _
dateString, dateResult, dateResult.Kind)
Else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
End If
' Attempt to convert a string in improper ISO 8601 format.
dateString = "03/01/2009T10:00:00-5:00"
If DateTime.TryParse(dateString, culture, styles, dateResult)
Console.WriteLine("{0} converted to {1} {2}.", _
dateString, dateResult, dateResult.Kind)
Else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
End If
' Assume a date and time string formatted for the fr-FR culture is the local
' time and convert it to UTC.
dateString = "2008-03-01 10:00"
culture = CultureInfo.CreateSpecificCulture("fr-FR")
styles = DateTimeStyles.AdjustToUniversal Or DateTimeStyles.AssumeLocal
If DateTime.TryParse(dateString, culture, styles, dateResult)
Console.WriteLine("{0} converted to {1} {2}.", _
dateString, dateResult, dateResult.Kind)
Else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
End If
End Sub
End Module
' The example displays the following output to the console:
' 03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Unspecified.
' 03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Local.
' 2009/03/01T10:00:00-5:00 converted to 3/1/2009 7:00:00 AM Local.
' Unable to convert 03/01/2009T10:00:00-5:00 to a date and time.
' 2008-03-01 10:00 converted to 3/1/2008 6:00:00 PM Utc.
注釈
このメソッドは DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime) 、日付、時刻、タイム ゾーンの情報を含めることができる文字列を解析します。 これはメソッドに DateTime.Parse(String, IFormatProvider, DateTimeStyles) 似ていますが DateTime.TryParse(String, DateTime) 、変換が失敗してもメソッドが例外をスローしない点が異なります。
このメソッドは、認識されないデータを無視し、完全に解析 s
しようとします。 時刻が含まれているが日付が含まれている場合s
、メソッドは既定で現在の日付を置き換えるか、フラグがNoCurrentDateDefault含まれている場合styles
は置き換えられますDateTime.Date.MinValue
。 日付が含まれているが時刻が含まれている場合 s
は、午前 12 時 00 分が既定の時刻として使用されます。 日付が存在するが、その年のコンポーネントが 2 桁のみで構成されている場合、プロパティの値に基づいて、パラメーターの現在のカレンダーで provider
年に Calendar.TwoDigitYearMax 変換されます。 先頭、内側、または末尾の s
空白文字はすべて無視されます。 日付と時刻は、先頭と末尾の NUMBER SIGN 文字 ('#'、U+0023) のペアで角かっこで囲み、1 つ以上の NULL 文字 (U+0000) で後ろに続けることができます。
日付と時刻の要素に対する特定の有効な形式、および日付と時刻で使用される名前と記号は、パラメーターによって provider
定義されます。パラメーターには、次のいずれかを指定できます。
CultureInfoパラメーターで書式設定が使用されるカルチャを
s
表すオブジェクト。 プロパティによって返されるオブジェクトは DateTimeFormatInfo 、 CultureInfo.DateTimeFormat で使用されるs
書式設定を定義します。で DateTimeFormatInfo 使用
s
される書式を定義するオブジェクト。カスタムの IFormatProvider 実装。 そのメソッドは IFormatProvider.GetFormat 、で DateTimeFormatInfo 使用
s
される書式設定を定義するオブジェクトを返します。
provider
が null
の場合は、現在のカルチャが使用されます。
現在のカレンダーの閏年の閏日の文字列表現である場合 s
、メソッドは正常に解析されます s
。 現在のカレンダーの閏年以外の年の閏日のprovider
文字列表現である場合s
、解析操作は失敗し、メソッドは返しますfalse
。
パラメーターは styles
、解析された文字列の正確な解釈と、解析操作で処理する方法を定義します。 次の表に示すように、列挙体の DateTimeStyles 1 つ以上のメンバーを指定できます。
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 | 有効ですが、この値は無視されます。 の日付と時刻の要素 s では、内側の空白を使用できます。 |
AllowLeadingWhite | 有効ですが、この値は無視されます。 の日付と時刻の要素 s では、先頭の空白を使用できます。 |
AllowTrailingWhite | 有効ですが、この値は無視されます。 の日付と時刻の要素 s では、末尾の空白を使用できます。 |
AllowWhiteSpaces | 先頭、内側、末尾の s 空白を含めることができます。 これは既定の動作です。 次のようなDateTimeStyles.Noneより制限の厳しいDateTimeStyles列挙値を指定してオーバーライドすることはできません。 |
AssumeLocal | タイム ゾーン情報がない場合 s は、ローカル時刻を表すと見なされることを指定します。 フラグが DateTimeStyles.AdjustToUniversal 存在しない限り、 Kind 戻り DateTime 値のプロパティは DateTimeKind.Local. |
AssumeUniversal | タイム ゾーン情報がない場合 s は、UTC を表すと見なされることを指定します。 フラグが DateTimeStyles.AdjustToUniversal 存在しない限り、メソッドは返された DateTime 値を UTC から現地時刻に変換し、その Kind プロパティを DateTimeKind.Local. |
None | 有効ですが、この値は無視されます。 |
RoundtripKind | タイム ゾーン情報を含む文字列の場合は、日付と時刻の文字列のプロパティが設定DateTimeKind.Localされた値KindへのDateTime変換を回避しようとします。 通常、このような文字列は、"o"、"r"、または "u" 標準書式指定子を使用してメソッドを呼び出 DateTime.ToString(String) すことによって作成されます。 |
タイム ゾーン情報が含まれている場合s
、フラグが特に指定されていない限り、DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime)メソッドはプロパティをDateTimeKind.Unspecified持つKind値をstyles
返DateTimeします。 タイム ゾーンまたはタイム ゾーン オフセット情報が含まれている場合 s
、メソッドは DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime) 必要な時間変換を実行し、次のいずれかを返します。
DateTime日付と時刻に現地時刻が反映され、Kindプロパティが DateTimeKind.Local.
または、フラグが含まれている場合
styles
は、DateTime日付と時刻が UTC を反映し、Kindプロパティが DateTimeKind.Utc.AdjustToUniversal
この動作は、フラグを使用 DateTimeStyles.RoundtripKind してオーバーライドできます。
カスタム カルチャの解析
カスタム カルチャ用に生成された日付と時刻の文字列を解析する場合は、メソッドの代わりにメソッドをTryParse使用TryParseExactして、解析操作が成功する可能性を高めます。 カスタム カルチャの日付と時刻の文字列は、複雑で解析が困難な場合があります。 このメソッドは TryParse 、いくつかの暗黙的な解析パターンを使用して文字列を解析しようとしますが、これらはすべて失敗する可能性があります。 これに対し、この TryParseExact メソッドでは、成功する可能性が高い 1 つ以上の正確な解析パターンを明示的に指定する必要があります。
カスタム カルチャの詳細については、クラスを System.Globalization.CultureAndRegionInfoBuilder 参照してください。
注意 (呼び出し元)
書式設定は、パラメーターによって提供される現在 DateTimeFormatInfo のオブジェクトのプロパティの影響を provider
受けます。 メソッドがTryParse予期せず失敗し、現在DateSeparatorとTimeSeparatorプロパティが同じ値に設定されている場合に戻るFalse
可能性があります。
こちらもご覧ください
適用対象
TryParse(String, IFormatProvider, DateTime)
文字列を値に解析しようとします。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] DateTime % result) = IParsable<DateTime>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out DateTime result);
static member TryParse : string * IFormatProvider * DateTime -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As DateTime) As Boolean
パラメーター
- s
- String
- provider
- IFormatProvider
- result
- DateTime
戻り値
true
正常に解析された場合は .。それ以外の場合s
false
は .
適用対象
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTime)
文字の範囲を解析して値を取得しようとします。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] DateTime % result) = ISpanParsable<DateTime>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out DateTime result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * DateTime -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As DateTime) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
- provider
- IFormatProvider
- result
- DateTime
戻り値
true
正常に解析された場合は .。それ以外の場合s
false
は .
適用対象
TryParse(ReadOnlySpan<Char>, DateTime)
指定した日付と時刻の文字スパンを、それと等価な DateTime に変換し、変換が成功したかどうかを示す値を返します。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] DateTime % result);
public static bool TryParse (ReadOnlySpan<char> s, out DateTime result);
static member TryParse : ReadOnlySpan<char> * DateTime -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As DateTime) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
変換する日付と時刻を格納した文字列。
- result
- DateTime
このメソッドから制御が戻るときに、変換が成功した場合は日付と時刻に相当する値を格納し、変換に失敗した場合は DateTime.MinValue を格納DateTimes
します。 s
パラメーターが null
の場合、空の文字列 ("") の場合、または日付と時刻を表す有効な文字列形式が含まれていない場合は、変換に失敗します。 このパラメーターは初期化せずに渡されます。
戻り値
s
パラメーターが正常に変換された場合は true
。それ以外の場合は false
。
適用対象
TryParse(String, DateTime)
指定した文字列形式の日時を対応する DateTime 表現に変換し、変換に成功したかどうかを示す値を返します。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] DateTime % result);
public static bool TryParse (string s, out DateTime result);
public static bool TryParse (string? s, out DateTime result);
static member TryParse : string * DateTime -> bool
Public Shared Function TryParse (s As String, ByRef result As DateTime) As Boolean
パラメーター
- s
- String
変換する日付と時刻を格納した文字列。
- result
- DateTime
このメソッドから制御が戻るときに、変換が成功した場合は日付と時刻に相当する値を格納し、変換に失敗した場合は DateTime.MinValue を格納DateTimes
します。 s
パラメーターが null
の場合、空の文字列 ("") の場合、または日付と時刻を表す有効な文字列形式が含まれていない場合は、変換に失敗します。 このパラメーターは初期化せずに渡されます。
戻り値
s
パラメーターが正常に変換された場合は true
。それ以外の場合は false
。
例
次の例では、多数の日付と時刻の文字列をメソッドに DateTime.TryParse(String, DateTime) 渡します。
using namespace System;
using namespace System::Globalization;
void main()
{
array<String^>^ dateStrings = { "05/01/2009 14:57:32.8", "2009-05-01 14:57:32.8",
"2009-05-01T14:57:32.8375298-04:00",
"5/01/2008 14:57:32.80 -07:00",
"1 May 2008 2:57:32.8 PM", "16-05-2009 1:00:32 PM",
"Fri, 15 May 2009 20:10:57 GMT" };
DateTime dateValue;
Console::WriteLine("Attempting to parse strings using {0} culture.",
CultureInfo::CurrentCulture->Name);
for each (String^ dateString in dateStrings)
{
if (DateTime::TryParse(dateString, dateValue))
Console::WriteLine(" Converted '{0}' to {1} ({2}).", dateString,
dateValue, dateValue.Kind);
else
Console::WriteLine(" Unable to parse '{0}'.", dateString);
}
}
// The example displays the following output:
// Attempting to parse strings using en-US culture.
// Converted '05/01/2009 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
// Converted '2009-05-01 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
// Converted '2009-05-01T14:57:32.8375298-04:00' to 5/1/2009 11:57:32 AM (Local).
// Converted '5/01/2008 14:57:32.80 -07:00' to 5/1/2008 2:57:32 PM (Local).
// Converted '1 May 2008 2:57:32.8 PM' to 5/1/2008 2:57:32 PM (Unspecified).
// Unable to parse '16-05-2009 1:00:32 PM'.
// Converted 'Fri, 15 May 2009 20:10:57 GMT' to 5/15/2009 1:10:57 PM (Local).
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] dateStrings = {"05/01/2009 14:57:32.8", "2009-05-01 14:57:32.8",
"2009-05-01T14:57:32.8375298-04:00", "5/01/2008",
"5/01/2008 14:57:32.80 -07:00",
"1 May 2008 2:57:32.8 PM", "16-05-2009 1:00:32 PM",
"Fri, 15 May 2009 20:10:57 GMT" };
DateTime dateValue;
Console.WriteLine("Attempting to parse strings using {0} culture.",
CultureInfo.CurrentCulture.Name);
foreach (string dateString in dateStrings)
{
if (DateTime.TryParse(dateString, out dateValue))
Console.WriteLine(" Converted '{0}' to {1} ({2}).", dateString,
dateValue, dateValue.Kind);
else
Console.WriteLine(" Unable to parse '{0}'.", dateString);
}
}
}
// The example displays output like the following:
// Attempting to parse strings using en-US culture.
// Converted '05/01/2009 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
// Converted '2009-05-01 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
// Converted '2009-05-01T14:57:32.8375298-04:00' to 5/1/2009 11:57:32 AM (Local).
//
// Converted '5/01/2008' to 5/1/2008 12:00:00 AM (Unspecified).
// Converted '5/01/2008 14:57:32.80 -07:00' to 5/1/2008 2:57:32 PM (Local).
// Converted '1 May 2008 2:57:32.8 PM' to 5/1/2008 2:57:32 PM (Unspecified).
// Unable to parse '16-05-2009 1:00:32 PM'.
// Converted 'Fri, 15 May 2009 20:10:57 GMT' to 5/15/2009 1:10:57 PM (Local).
open System
open System.Globalization
let dateStrings =
[ "05/01/2009 14:57:32.8"; "2009-05-01 14:57:32.8"
"2009-05-01T14:57:32.8375298-04:00"; "5/01/2008"
"5/01/2008 14:57:32.80 -07:00"
"1 May 2008 2:57:32.8 PM"; "16-05-2009 1:00:32 PM"
"Fri, 15 May 2009 20:10:57 GMT" ]
printfn $"Attempting to parse strings using {CultureInfo.CurrentCulture.Name} culture."
for dateString in dateStrings do
match DateTime.TryParse dateString with
| true, dateValue ->
printfn $" Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
| _ ->
printfn $" Unable to parse '{dateString}'."
// The example displays output like the following:
// Attempting to parse strings using en-US culture.
// Converted '05/01/2009 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
// Converted '2009-05-01 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
// Converted '2009-05-01T14:57:32.8375298-04:00' to 5/1/2009 11:57:32 AM (Local).
// Converted '5/01/2008' to 5/1/2008 12:00:00 AM (Unspecified).
// Converted '5/01/2008 14:57:32.80 -07:00' to 5/1/2008 2:57:32 PM (Local).
// Converted '1 May 2008 2:57:32.8 PM' to 5/1/2008 2:57:32 PM (Unspecified).
// Unable to parse '16-05-2009 1:00:32 PM'.
// Converted 'Fri, 15 May 2009 20:10:57 GMT' to 5/15/2009 1:10:57 PM (Local).
Imports System.Globalization
Public Module Example
Public Sub Main()
Dim dateStrings() As String = {"05/01/2009 14:57:32.8", "2009-05-01 14:57:32.8",
"2009-05-01T14:57:32.8375298-04:00", "5/01/2008",
"5/01/2008 14:57:32.80 -07:00",
"1 May 2008 2:57:32.8 PM", "16-05-2009 1:00:32 PM",
"Fri, 15 May 2009 20:10:57 GMT"}
Dim dateValue As Date
Console.WriteLine("Attempting to parse strings using {0} culture.", _
CultureInfo.CurrentCulture.Name)
For Each dateString As String In dateStrings
If Date.TryParse(dateString, dateValue) Then
Console.WriteLine(" Converted '{0}' to {1} ({2}).", dateString, _
dateValue, dateValue.Kind)
Else
Console.WriteLine(" Unable to parse '{0}'.", dateString)
End If
Next
End Sub
End Module
' The example displays output like the following:
' Attempting to parse strings using en-US culture.
' Converted '05/01/2009 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
' Converted '2009-05-01 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
' Converted '2009-05-01T14:57:32.8375298-04:00' to 5/1/2009 11:57:32 AM (Local).
'
' Converted '5/01/2008' to 5/1/2008 12:00:00 AM (Unspecified).
' Converted '5/01/2008 14:57:32.80 -07:00' to 5/1/2008 2:57:32 PM (Local).
' Converted '1 May 2008 2:57:32.8 PM' to 5/1/2008 2:57:32 PM (Unspecified).
' Unable to parse '16-05-2009 1:00:32 PM'.
' Converted 'Fri, 15 May 2009 20:10:57 GMT' to 5/15/2009 1:10:57 PM (Local).
注釈
メソッドは DateTime.TryParse(String, DateTime) メソッドに DateTime.Parse(String) 似ていますが TryParse(String, DateTime) 、変換が失敗してもメソッドが例外をスローしない点が異なります。
文字列 s
は、現在のカルチャによって暗黙的に提供される、現在 DateTimeFormatInfo のオブジェクトの書式設定情報を使用して解析されます。
このメソッドは、認識できないデータ (可能な場合) を無視し、不足している月、日、年の情報に現在の日付を入力します。 日付のみを含み、時刻がない場合 s
、このメソッドは時刻が午前 0 時 12 分 00 分であると想定します。 年が 2 桁の日付コンポーネントが含まれている場合 s
、プロパティの値に基づいて、現在のカルチャの現在のカレンダーで年に Calendar.TwoDigitYearMax 変換されます。 先頭、内側、または末尾の s
空白文字はすべて無視されます。 日付と時刻は、先頭と末尾の NUMBER SIGN 文字 ('#'、U+0023) のペアで角かっこで囲み、1 つ以上の NULL 文字 (U+0000) で後ろに続けることができます。
メソッドは DateTime.TryParse(String, DateTime) 現在のカルチャの書式設定規則を使用して日付と時刻の文字列表現を解析しようとするため、異なるカルチャ間で特定の文字列を解析しようとすると、失敗するか、異なる結果が返される可能性があります。 特定の日付と時刻の形式が異なるロケールで解析される場合は、メソッドまたはメソッドのいずれかのオーバーロードTryParseExactを使用DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime)し、書式指定子を指定します。
現在のカレンダーの閏年の閏日の文字列表現である場合 s
、メソッドは正常に解析されます s
。 現在のカルチャの現在のカレンダーの閏年以外の閏日の文字列表現である場合 s
、解析操作は失敗し、メソッドは返します false
。
タイム ゾーン情報が含まれている場合は、result
メソッドが戻るときにプロパティKindDateTimeKind.Unspecifiedを持つ値が含まれますDateTime。s
解析対象の文字列にタイム ゾーン情報が含まれている場合は、result
メソッドが戻るときにプロパティDateTimeKind.Localを持つKind値が含まれますDateTime。
注意 (呼び出し元)
書式設定は、現在DateTimeFormatInfoのオブジェクトのプロパティの影響を受けます。既定では、コントロール パネルの [地域と言語のオプション] 項目から派生します。 メソッドがTryParse予期せず失敗し、現在DateSeparatorとTimeSeparatorプロパティが同じ値に設定されている場合に戻るFalse
可能性があります。
こちらもご覧ください
- Parse
- CultureInfo
- DateTimeFormatInfo
- .NET Framework における日付と時刻文字列の解析の解析
- 標準の日時書式指定文字列
- カスタム日時書式指定文字列
- サンプル: .NET Core WinForms 書式設定ユーティリティ (C#)
- サンプル: .NET Core WinForms 書式設定ユーティリティ (Visual Basic)
適用対象
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime)
指定したカルチャ固有の書式情報と書式スタイルを使用して、日付と時刻のスパン表現を、それと等価な DateTime に変換し、変換が成功したかどうかを示す値を返します。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTime % result);
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, System.Globalization.DateTimeStyles styles, out DateTime result);
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider provider, System.Globalization.DateTimeStyles styles, out DateTime result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * System.Globalization.DateTimeStyles * DateTime -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTime) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
変換する日付と時刻を表す文字を含むスパン。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- styles
- DateTimeStyles
現在のタイム ゾーンまたは現在の日付と関連させて、解析された日付をどのように解釈するかを定義する列挙値のビットごとの組み合わせ。 通常指定する値は、None です。
- result
- DateTime
このメソッドから制御が戻るときに、変換が成功した場合は日付と時刻に相当する値を格納し、変換に失敗した場合は DateTime.MinValue を格納DateTimes
します。 s
パラメーターが null
の場合、空の文字列 ("") の場合、または日付と時刻を表す有効な文字列形式が含まれていない場合は、変換に失敗します。 このパラメーターは初期化せずに渡されます。
戻り値
s
パラメーターが正常に変換された場合は true
。それ以外の場合は false
。