DateTimeOffset.TryParseExact 메서드

정의

날짜 및 시간에 대한 지정된 문자열 표현을 해당 DateTimeOffset으로 변환합니다. 문자열 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

오버로드

TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset)

지정된 형식 배열, 문화권별 형식 정보 및 스타일을 사용하여 지정된 날짜와 시간의 문자열 표현을 해당 DateTimeOffset 표현으로 변환합니다. 문자열 표시의 형식은 지정된 형식 중 하나와 정확히 일치해야 합니다.

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset)

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 날짜 및 시간의 표현을 해당 DateTimeOffset으로 변환합니다. 날짜 및 시간 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

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

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 문자 범위 내의 날짜 및 시간 표현을 해당하는 DateTimeOffset로 변환합니다. 날짜 및 시간 표현의 형식은 지정된 형식 중 하나와 정확히 일치해야 합니다.

TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset)

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 날짜 및 시간의 지정된 문자열 표현을 해당 DateTimeOffset으로 변환합니다. 문자열 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset)

지정된 형식 배열, 문화권별 형식 정보 및 스타일을 사용하여 지정된 날짜와 시간의 문자열 표현을 해당 DateTimeOffset 표현으로 변환합니다. 문자열 표시의 형식은 지정된 형식 중 하나와 정확히 일치해야 합니다.

public:
 static bool TryParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParseExact (string input, string[] formats, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParseExact (string? input, string?[]? formats, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParseExact : string * string[] * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParseExact (input As String, formats As String(), formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

매개 변수

input
String

변환할 날짜 및 시간이 포함된 문자열입니다.

formats
String[]

input의 예상 형식을 정의하는 배열입니다.

formatProvider
IFormatProvider

input에 대한 문화권별 형식 지정 정보를 제공하는 개체입니다.

styles
DateTimeStyles

input에 사용할 수 있는 형식을 나타내는 열거형 값의 비트 조합입니다. 지정할 일반적인 값은 None입니다.

result
DateTimeOffset

메서드가 반환될 때 변환에 성공한 경우 의 input날짜 및 시간에 해당하는 를 포함하거나 변환에 실패한 경우 DateTimeOffset.MinValue를 포함합니다DateTimeOffset. input에 날짜 및 시간의 유효한 문자열 표현이 포함되어 있지 않거나 format에 정의된 예상 형식에 따른 날짜 및 시간이 포함되어 있지 않거나 formatsnull이면 변환이 실패합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

반환

input 매개 변수가 변환되었으면 true이고, 변환되지 않았으면 false입니다.

예외

styles에 정의되지 않은 DateTimeStyles 값이 들어 있습니다.

또는

NoCurrentDateDefault은 지원되지 않습니다.

또는

styles에 함께 사용할 수 없는 DateTimeStyles 값이 들어 있습니다.

예제

다음 예제에서는 날짜 및 시간 및 오프셋 값의 문자열 표현에 대해 여러 입력 형식을 정의한 다음 사용자가 입력한 문자열을 메서드에 TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset) 전달합니다.

TextReader conIn = Console.In;
TextWriter conOut = Console.Out;
int tries = 0;
string input = String.Empty;

string[] formats = new string[] {"M/dd/yyyy HH:m zzz", "MM/dd/yyyy HH:m zzz",
                                 "M/d/yyyy HH:m zzz", "MM/d/yyyy HH:m zzz",
                                 "M/dd/yy HH:m zzz", "MM/dd/yy HH:m zzz",
                                 "M/d/yy HH:m zzz", "MM/d/yy HH:m zzz",
                                 "M/dd/yyyy H:m zzz", "MM/dd/yyyy H:m zzz",
                                 "M/d/yyyy H:m zzz", "MM/d/yyyy H:m zzz",
                                 "M/dd/yy H:m zzz", "MM/dd/yy H:m zzz",
                                 "M/d/yy H:m zzz", "MM/d/yy H:m zzz",
                                 "M/dd/yyyy HH:mm zzz", "MM/dd/yyyy HH:mm zzz",
                                 "M/d/yyyy HH:mm zzz", "MM/d/yyyy HH:mm zzz",
                                 "M/dd/yy HH:mm zzz", "MM/dd/yy HH:mm zzz",
                                 "M/d/yy HH:mm zzz", "MM/d/yy HH:mm zzz",
                                 "M/dd/yyyy H:mm zzz", "MM/dd/yyyy H:mm zzz",
                                 "M/d/yyyy H:mm zzz", "MM/d/yyyy H:mm zzz",
                                 "M/dd/yy H:mm zzz", "MM/dd/yy H:mm zzz",
                                 "M/d/yy H:mm zzz", "MM/d/yy H:mm zzz"};
IFormatProvider provider = CultureInfo.InvariantCulture.DateTimeFormat;
DateTimeOffset result;

do {
   conOut.WriteLine("Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),");
   conOut.Write("Then press Enter: ");
   input = conIn.ReadLine();
   conOut.WriteLine();
   if (DateTimeOffset.TryParseExact(input, formats, provider,
                                   DateTimeStyles.AllowWhiteSpaces,
                                   out result))
   {
      break;
   }
   else
   {
      Console.WriteLine("Unable to parse {0}.", input);
      tries++;
   }
} while (tries < 3);
if (tries >= 3)
   Console.WriteLine("Exiting application without parsing {0}", input);
else
   Console.WriteLine("{0} was converted to {1}", input, result.ToString());
// Some successful sample interactions with the user might appear as follows:
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/08/2007 6:54 -6:00
//
//    12/08/2007 6:54 -6:00 was converted to 12/8/2007 6:54:00 AM -06:00
//
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/8/2007 06:54 -06:00
//
//    12/8/2007 06:54 -06:00 was converted to 12/8/2007 6:54:00 AM -06:00
//
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/5/07 6:54 -6:00
//
//    12/5/07 6:54 -6:00 was converted to 12/5/2007 6:54:00 AM -06:00
let mutable result = None    
let mutable tries = 0
let mutable input = ""

let formats = 
    [| "M/dd/yyyy HH:m zzz"; "MM/dd/yyyy HH:m zzz"
       "M/d/yyyy HH:m zzz"; "MM/d/yyyy HH:m zzz"
       "M/dd/yy HH:m zzz"; "MM/dd/yy HH:m zzz"
       "M/d/yy HH:m zzz"; "MM/d/yy HH:m zzz"
       "M/dd/yyyy H:m zzz"; "MM/dd/yyyy H:m zzz"
       "M/d/yyyy H:m zzz"; "MM/d/yyyy H:m zzz"
       "M/dd/yy H:m zzz"; "MM/dd/yy H:m zzz"
       "M/d/yy H:m zzz"; "MM/d/yy H:m zzz"
       "M/dd/yyyy HH:mm zzz"; "MM/dd/yyyy HH:mm zzz"
       "M/d/yyyy HH:mm zzz"; "MM/d/yyyy HH:mm zzz"
       "M/dd/yy HH:mm zzz"; "MM/dd/yy HH:mm zzz"
       "M/d/yy HH:mm zzz"; "MM/d/yy HH:mm zzz"
       "M/dd/yyyy H:mm zzz"; "MM/dd/yyyy H:mm zzz"
       "M/d/yyyy H:mm zzz"; "MM/d/yyyy H:mm zzz"
       "M/dd/yy H:mm zzz"; "MM/dd/yy H:mm zzz"
       "M/d/yy H:mm zzz"; "MM/d/yy H:mm zzz" |]
let provider = CultureInfo.InvariantCulture.DateTimeFormat

while tries < 3 && result.IsNone do
    printfn "Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),"
    printf "Then press Enter: "
    input <- stdin.ReadLine()
    printfn ""
    match DateTimeOffset.TryParseExact(input, formats, provider, DateTimeStyles.AllowWhiteSpaces) with
    | true, dto ->
        result <- Some dto
    | _ ->
        printfn $"Unable to parse {input}."
    tries <- tries + 1

match result with
| Some result ->
    printfn $"{input} was converted to {result}"
| None ->
    printfn $"Exiting application without parsing {input}"

// Some successful sample interactions with the user might appear as follows:
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/08/2007 6:54 -6:00
//
//    12/08/2007 6:54 -6:00 was converted to 12/8/2007 6:54:00 AM -06:00
//
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/8/2007 06:54 -06:00
//
//    12/8/2007 06:54 -06:00 was converted to 12/8/2007 6:54:00 AM -06:00
//
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/5/07 6:54 -6:00
//
//    12/5/07 6:54 -6:00 was converted to 12/5/2007 6:54:00 AM -06:00
 Dim conIn As TextReader = Console.In
 Dim conOut As TextWriter = Console.Out
 Dim tries As Integer = 0
 Dim input As String = String.Empty
 Dim formats() As String = {"M/dd/yyyy HH:m zzz", "MM/dd/yyyy HH:m zzz", _
                            "M/d/yyyy HH:m zzz", "MM/d/yyyy HH:m zzz", _
                            "M/dd/yy HH:m zzz", "MM/dd/yy HH:m zzz", _
                            "M/d/yy HH:m zzz", "MM/d/yy HH:m zzz", _                                 
                            "M/dd/yyyy H:m zzz", "MM/dd/yyyy H:m zzz", _
                            "M/d/yyyy H:m zzz", "MM/d/yyyy H:m zzz", _
                            "M/dd/yy H:m zzz", "MM/dd/yy H:m zzz", _
                            "M/d/yy H:m zzz", "MM/d/yy H:m zzz", _                               
                            "M/dd/yyyy HH:mm zzz", "MM/dd/yyyy HH:mm zzz", _
                            "M/d/yyyy HH:mm zzz", "MM/d/yyyy HH:mm zzz", _
                            "M/dd/yy HH:mm zzz", "MM/dd/yy HH:mm zzz", _
                            "M/d/yy HH:mm zzz", "MM/d/yy HH:mm zzz", _                                 
                            "M/dd/yyyy H:mm zzz", "MM/dd/yyyy H:mm zzz", _
                            "M/d/yyyy H:mm zzz", "MM/d/yyyy H:mm zzz", _
                            "M/dd/yy H:mm zzz", "MM/dd/yy H:mm zzz", _
                            "M/d/yy H:mm zzz", "MM/d/yy H:mm zzz"}   
 Dim provider As IFormatProvider = CultureInfo.InvariantCulture.DateTimeFormat
 Dim result As DateTimeOffset

 Do 
    conOut.WriteLine("Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),")
    conOut.Write("Then press Enter: ")
    input = conIn.ReadLine()
    conOut.WriteLine() 
    If DateTimeOffset.TryParseExact(input, formats, provider, _
                                    DateTimeStyles.AllowWhiteSpaces, _
                                    result) Then
       Exit Do
    Else
       Console.WriteLine("Unable to parse {0}.", input)      
       tries += 1
    End If
 Loop While tries < 3
 If tries >= 3 Then
    Console.WriteLine("Exiting application without parsing {0}", input)
 Else
    Console.WriteLine("{0} was converted to {1}", input, result.ToString())                                                     
 End If 
 ' Some successful sample interactions with the user might appear as follows:
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/08/2007 6:54 -6:00
 '    
 '    12/08/2007 6:54 -6:00 was converted to 12/8/2007 6:54:00 AM -06:00         
 '    
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/8/2007 06:54 -06:00
 '    
 '    12/8/2007 06:54 -06:00 was converted to 12/8/2007 6:54:00 AM -06:00
 '    
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/5/07 6:54 -6:00
 '    
 '    12/5/07 6:54 -6:00 was converted to 12/5/2007 6:54:00 AM -06:00

설명

메서드는 TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset) 배열에 할당된 패턴 중 하나와 일치하는 날짜의 문자열 표현을 formats 구문 분석합니다. 문자열이 매개 변수로 input 정의된 styles 변형과 이러한 패턴 중 하나와 일치하지 않으면 구문 분석 작업이 실패하고 메서드가 를 반환합니다 false. 형식 지정자를 포함하는 여러 문자열과 비교하는 input 것 외에도 이 오버로드는 메서드와 동일하게 DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 동작합니다.

매개 변수는 formats 요소에 단일 표준 형식 지정자 또는 의 가능한 패턴을 정의하는 하나 이상의 사용자 지정 형식 지정자가 포함된 문자열 배열입니다 input. 유효한 서식 코드에 대한 자세한 내용은 표준 날짜 및 시간 형식 문자열 및사용자 지정 날짜 및 시간 형식 문자열을 참조하세요. 의 일치하는 요소에 formats 오프셋이 있어야 input함을 나타내는 , zz또는 zzz 사용자 지정 형식 지정자가 포함된 z경우 해당 오프셋에는 음수 기호 또는 양수 기호가 포함되어야 합니다. 기호가 없으면 구문 분석 작업이 실패하고 메서드가 를 반환합니다 false.

중요

formats 이 오버로드의 매개 변수를 사용하여 여러 형식을 지정하면 많은 사용자가 날짜와 시간을 입력할 때 발생하는 불만을 줄일 수 있습니다. 특히 여러 개의 입력된 패턴을 정의 하는 기능 포함 하거나 부족 한 월, 일, 시간, 분 및 초를 앞에 오는 0 수 있는 날짜와 시간 표현을 처리 하도록 애플리케이션을 수 있습니다. 이 예제에서는 이에 대한 그림을 제공합니다.

에서 일치하는 요소에 formats 날짜가 input 포함되어야 하지만 시간이 없는 경우 결과 DateTimeOffset 개체에 자정(0:00:00)의 시간이 할당됩니다. 의 일치하는 요소에서 formats 입력에 날짜가 아닌 시간이 포함되어야 하는 경우 결과 DateTimeOffset 개체에 로컬 시스템의 현재 날짜가 할당됩니다. 의 일치하는 요소에 formats 오프셋이 input 포함되지 않아도 되는 경우 결과 DateTimeOffset 개체의 오프셋은 매개 변수의 styles 값에 따라 달라집니다. 가 포함된 AssumeLocal경우 styles 로컬 표준 시간대의 오프셋이 개체에 DateTimeOffset 할당됩니다. 가 포함된 AssumeUniversal경우 styles UTC(협정 세계시) 오프셋 또는 +00:00이 개체에 DateTimeOffset 할당됩니다. 두 값을 모두 지정하지 않으면 현지 표준 시간대의 오프셋이 사용됩니다.

에 사용되는 input 특정 날짜 및 시간 기호와 문자열은 매개 변수로 formatProvider 정의됩니다. 의 일치 요소가 표준 형식 지정자 문자열인 경우 의 inputformats 정확한 패턴도 마찬가지입니다. 매개 변수는 formatProvider 다음 중 하나일 수 있습니다.

가 이 nullCultureInfoformatprovider 현재 문화권에 해당하는 개체가 사용됩니다.

매개 변수는 styles 입력 문자열에 공백이 허용되는지 여부를 정의하고, 명시적 오프셋 구성 요소가 없는 문자열을 구문 분석하는 방법을 나타내며, 구문 분석 작업의 일부로 UTC 변환을 지원합니다. 를 제외한 NoCurrentDateDefault열거형의 DateTimeStyles 모든 멤버가 지원됩니다. 다음 표에서는 지원되는 각 멤버의 효과를 나열합니다.

DateTimeStyles 멤버 동작
AdjustToUniversal 및 를 input 구문 분석하고 필요한 경우 UTC로 변환합니다. 문자열을 구문 분석한 다음 반환 DateTimeOffset 된 개체의 메서드를 DateTimeOffset.ToUniversalTime 호출하는 것과 같습니다.
AssumeLocal 의 일치하는 요소에 formats 오프셋 값이 input 포함되지 않아도 되는 경우 반환 DateTimeOffset 된 개체에는 현지 표준 시간대의 오프셋이 제공됩니다. 기본값입니다.
AssumeUniversal 의 일치하는 요소에 formats 오프셋 값이 input 포함되지 않아도 되는 경우 반환 DateTimeOffset 된 개체에는 UTC 오프셋(+00:00)이 제공됩니다.
AllowInnerWhite input 요소에서 지정하지 않은 내부 공백을 포함할 수 있습니다 formats. 날짜 및 시간 구성 요소와 개별 구성 요소(오프셋 제외) 내에 추가 공백이 표시될 수 있으며 문자열을 구문 분석할 때 무시됩니다.
AllowLeadingWhite input 요소에서 지정하지 않은 선행 공백을 포함할 수 있습니다 formats. 문자열을 구문 분석할 때 무시됩니다.
AllowTrailingWhite input 요소에서 지정하지 않은 후행 공백을 포함할 수 있습니다 formats. 문자열을 구문 분석할 때 무시됩니다.
AllowWhiteSpaces 의 요소에서 input 지정하지 않은 선행, 후행 및 내부 공백을 포함할 수 있습니다 formats. 문자열을 구문 분석할 때 일치하는 요소 formats 에 지정되지 않은 추가 공백 문자는 모두 무시됩니다.
None 에서 추가 공백이 허용되지 않음을 input나타냅니다. 일치가 성공하려면 의 특정 요소 formats 에 지정된 대로 공백이 정확하게 나타나야 합니다. 기본 동작입니다.
RoundtripKind 구조체에 속성이 DateTimeOffset 포함되어 Kind 있지 않으므로 아무런 효과가 없습니다.

호출자 참고

.NET Framework 4 TryParseExact 에서는 구문 분석할 문자열에 시간 구성 요소와 규약이 없는 AM/PM 지정자가 포함되어 있으면 를 반환 false 합니다. .NET Framework 3.5 이전 버전에서는 AM/PM 지정자가 무시됩니다.

적용 대상

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset)

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 날짜 및 시간의 표현을 해당 DateTimeOffset으로 변환합니다. 날짜 및 시간 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

public:
 static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

매개 변수

input
ReadOnlySpan<Char>

변환할 날짜 및 시간을 나타내는 문자를 포함하는 범위입니다.

format
ReadOnlySpan<Char>

input의 필수 형식을 정의하는 서식 지정자입니다.

formatProvider
IFormatProvider

input에 대한 문화권별 형식 지정 정보를 제공하는 개체입니다.

styles
DateTimeStyles

input에 사용할 수 있는 형식을 나타내는 열거형 값의 비트 조합입니다. 지정할 일반적인 값은 None입니다.

result
DateTimeOffset

메서드가 반환될 때 변환에 성공한 경우 의 input날짜 및 시간에 해당하는 를 포함하거나 변환에 실패한 경우 DateTimeOffset.MinValue를 포함합니다DateTimeOffset. 다음과 같은 경우 변환에 실패합니다.

반환

input 매개 변수가 변환되었으면 true이고, 변환되지 않았으면 false입니다.

예외

styles에 정의되지 않은 DateTimeStyles 값이 들어 있습니다. 또는 NoCurrentDateDefault는 지원되지 않습니다. 또는 styles에 함께 사용할 수 없는 DateTimeStyles 값이 들어 있습니다.

설명

이 오버로드는 변환이 DateTimeOffset.ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) 실패할 경우 이 메서드가 예외를 throw하지 않는다는 점을 제외하고 메서드와 같습니다. 매개 변수에 지정된 패턴과 정확히 일치해야 하는 날짜 및 시간의 표현을 format 구문 분석합니다. 이 패턴과 일치하지 않는 경우 input 매개 변수로 정의된 공백의 몇 가지 가능한 변형이 styles 있으면 구문 분석 작업이 실패하고 메서드가 를 반환합니다 false.

매개 변수는 format 단일 표준 형식 지정자 또는 의 필수 패턴을 정의하는 하나 이상의 사용자 지정 형식 지정자를 포함하는 문자 범위입니다 input. 유효한 서식 코드에 대한 자세한 내용은 표준 날짜 및 시간 형식 문자열 및사용자 지정 날짜 및 시간 형식 문자열을 참조하세요. 에 오프셋이 z있어야 input함을 나타내는 , zz또는 zzz 사용자 지정 형식 지정자가 포함된 경우 format 해당 오프셋에는 음수 기호 또는 양수 기호가 포함되어야 합니다. 기호가 없으면 구문 분석 작업이 실패하고 메서드가 를 반환합니다 false.

에 날짜가 input 포함되지만 시간이 포함되어야 하는 경우 format 결과 DateTimeOffset 개체에 자정(0:00:00)의 시간이 할당됩니다. input 에 시간이 포함되지만 날짜가 없는 경우 format 결과 DateTimeOffset 개체에 로컬 시스템의 현재 날짜가 할당됩니다. 에 오프셋이 input 포함되지 않은 경우 format 결과 DateTimeOffset 개체의 오프셋은 매개 변수의 styles 값에 따라 달라집니다. 가 포함된 AssumeLocal경우 styles 로컬 표준 시간대의 오프셋이 개체에 DateTimeOffset 할당됩니다. 가 포함된 AssumeUniversal경우 styles UTC(협정 세계시) 오프셋 또는 +00:00이 개체에 DateTimeOffset 할당됩니다. 두 값을 모두 지정하지 않으면 현지 표준 시간대의 오프셋이 사용됩니다.

에 사용되는 input 특정 날짜 및 시간 기호와 문자열은 매개 변수로 formatProvider 정의됩니다. 가 표준 형식 지정자 문자열인 경우 formatinput 정확한 패턴도 마찬가지입니다. 매개 변수는 formatProvider 다음 중 하나일 수 있습니다.

가 이 nullCultureInfoformatprovider 현재 문화권에 해당하는 개체가 사용됩니다.

매개 변수는 styles 입력 문자열에 공백이 허용되는지 여부를 정의하고, 명시적 오프셋 구성 요소가 없는 문자열을 구문 분석하는 방법을 나타내며, 구문 분석 작업의 일부로 UTC 변환을 지원합니다. 를 제외한 NoCurrentDateDefault열거형의 DateTimeStyles 모든 멤버가 지원됩니다. 다음 표에서는 지원되는 각 멤버의 효과를 나열합니다.

DateTimeStyles 멤버 동작
AdjustToUniversal 및 를 input 구문 분석하고 필요한 경우 UTC로 변환합니다. 날짜 및 시간 표현을 구문 분석한 다음 반환 DateTimeOffset 된 개체의 메서드를 DateTimeOffset.ToUniversalTime 호출하는 것과 같습니다.
AssumeLocal format 에 오프셋 값이 input 포함되어 있지 않으면 반환 DateTimeOffset 된 개체에 현지 표준 시간대의 오프셋이 제공됩니다. 기본 동작입니다.
AssumeUniversal format 에 오프셋 값이 input 포함되어 있지 않으면 반환 DateTimeOffset 된 개체에 UTC 오프셋(+00:00)이 제공됩니다.
AllowInnerWhite 형식으로 input 지정되지 않은 내부 공백을 포함할 수 있습니다. 날짜 및 시간 구성 요소와 오프셋 이외의 개별 구성 요소 내에 추가 공백이 표시될 수 있으며 문자열을 구문 분석할 때 무시됩니다.
AllowLeadingWhite 에서 input 지정하지 않은 선행 공백을 format포함할 수 있습니다. 문자열을 구문 분석할 때 무시됩니다.
AllowTrailingWhite 에서 input 지정하지 않은 후행 공백을 format포함할 수 있습니다. 문자열을 구문 분석할 때 무시됩니다.
AllowWhiteSpaces 에서 input 지정하지 않은 선행, 후행 및 내부 공백을 format포함할 수 있습니다. 문자열을 구문 분석할 때 에 format 지정되지 않은 추가 공백 문자는 모두 무시됩니다.
None 에서 추가 공백이 허용되지 않음을 input나타냅니다. 공백은 에 format지정된 대로 정확하게 표시되어야 합니다. 기본 동작입니다.
RoundtripKind 구조체에 속성이 DateTimeOffset 포함되어 Kind 있지 않으므로 아무런 효과가 없습니다.

적용 대상

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

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 문자 범위 내의 날짜 및 시간 표현을 해당하는 DateTimeOffset로 변환합니다. 날짜 및 시간 표현의 형식은 지정된 형식 중 하나와 정확히 일치해야 합니다.

public:
 static bool TryParseExact(ReadOnlySpan<char> input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParseExact (ReadOnlySpan<char> input, string?[]? formats, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), formats As String(), formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

매개 변수

input
ReadOnlySpan<Char>

변환할 날짜 및 시간을 나타내는 문자를 포함하는 범위입니다.

formats
String[]

input에 허용되는 형식을 정의하는 표준 또는 사용자 지정 형식 문자열 배열입니다.

formatProvider
IFormatProvider

input에 대한 문화권별 형식 지정 정보를 제공하는 개체입니다.

styles
DateTimeStyles

input에 사용할 수 있는 형식을 나타내는 열거형 값의 비트 조합입니다. 지정할 일반적인 값은 None입니다.

result
DateTimeOffset

메서드가 반환될 때 변환에 성공한 경우 의 input날짜 및 시간에 해당하는 를 포함하거나 변환에 실패한 경우 DateTimeOffset.MinValue를 포함합니다DateTimeOffset. 다음과 같은 경우 변환에 실패합니다.

반환

input 매개 변수가 변환되었으면 true이고, 변환되지 않았으면 false입니다.

예외

styles에 정의되지 않은 DateTimeStyles 값이 들어 있습니다. 또는 NoCurrentDateDefault는 지원되지 않습니다. 또는 styles에 함께 사용할 수 없는 DateTimeStyles 값이 들어 있습니다.

설명

이 메서드는 배열에 할당된 패턴 중 하나와 일치하는 날짜의 문자열 표현을 formats 구문 분석합니다. 이 매개 변수로 정의된 styles 변형과 이러한 패턴 중 하나와 일치하지 않으면 input 구문 분석 작업이 실패하고 메서드가 를 반환합니다false. 형식 지정자를 포함하는 여러 문자열과 비교하는 input 것 외에도 이 오버로드는 메서드와 동일하게 DateTimeOffset.ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles) 동작합니다.

매개 변수는 formats 요소에 단일 표준 형식 지정자 또는 의 가능한 패턴을 정의하는 하나 이상의 사용자 지정 형식 지정자가 포함된 문자열 배열입니다 input. 유효한 서식 코드에 대한 자세한 내용은 표준 날짜 및 시간 형식 문자열 및사용자 지정 날짜 및 시간 형식 문자열을 참조하세요. 의 일치하는 요소에 formats 오프셋이 있어야 input함을 나타내는 , zz또는 zzz 사용자 지정 형식 지정자가 포함된 z경우 해당 오프셋에는 음수 기호 또는 양수 기호가 포함되어야 합니다. 기호가 없으면 구문 분석 작업이 실패하고 메서드가 를 반환합니다 false.

중요

formats 이 오버로드의 매개 변수를 사용하여 여러 형식을 지정하면 많은 사용자가 날짜와 시간을 입력할 때 발생하는 불만을 줄일 수 있습니다. 특히 여러 개의 입력된 패턴을 정의 하는 기능 포함 하거나 부족 한 월, 일, 시간, 분 및 초를 앞에 오는 0 수 있는 날짜와 시간 표현을 처리 하도록 애플리케이션을 수 있습니다. 이 예제에서는 이에 대한 그림을 제공합니다.

에서 일치하는 요소에 formats 날짜가 input 포함되어야 하지만 시간이 없는 경우 결과 DateTimeOffset 개체에 자정(0:00:00)의 시간이 할당됩니다. 의 일치하는 요소에서 formats 입력에 날짜가 아닌 시간이 포함되어야 하는 경우 결과 DateTimeOffset 개체에 로컬 시스템의 현재 날짜가 할당됩니다. 의 일치하는 요소에 formats 오프셋이 input 포함되지 않아도 되는 경우 결과 DateTimeOffset 개체의 오프셋은 매개 변수의 styles 값에 따라 달라집니다. 가 포함된 DateTimeStyles.AssumeLocal경우 styles 로컬 표준 시간대의 오프셋이 개체에 DateTimeOffset 할당됩니다. 가 포함된 DateTimeStyles.AssumeUniversal경우 styles UTC(협정 세계시) 오프셋 또는 +00:00이 개체에 DateTimeOffset 할당됩니다. 두 값을 모두 지정하지 않으면 현지 표준 시간대의 오프셋이 사용됩니다.

input 사용되는 특정 날짜 및 시간 기호는 매개 변수에 formatProvider 의해 정의됩니다. 의 일치 요소가 표준 형식 지정자 문자열인 경우 의 inputformats 정확한 패턴도 마찬가지입니다. 매개 변수는 formatProvider 다음 중 하나일 수 있습니다.

가 이 nullCultureInfoformatprovider 현재 문화권에 해당하는 개체가 사용됩니다.

매개 변수는 styles 입력 문자열에 공백이 허용되는지 여부를 정의하고, 명시적 오프셋 구성 요소가 없는 문자열을 구문 분석하는 방법을 나타내며, 구문 분석 작업의 일부로 UTC 변환을 지원합니다. 를 제외한 NoCurrentDateDefault열거형의 DateTimeStyles 모든 멤버가 지원됩니다. 다음 표에서는 지원되는 각 멤버의 효과를 나열합니다.

DateTimeStyles 멤버 동작
AdjustToUniversal 및 를 input 구문 분석하고 필요한 경우 UTC로 변환합니다. 문자열을 구문 분석한 다음 반환 DateTimeOffset 된 개체의 메서드를 DateTimeOffset.ToUniversalTime 호출하는 것과 같습니다.
AssumeLocal 의 일치하는 요소에 formats 오프셋 값이 input 포함되지 않아도 되는 경우 반환 DateTimeOffset 된 개체에는 현지 표준 시간대의 오프셋이 제공됩니다. 기본값입니다.
AssumeUniversal 의 일치하는 요소에 formats 오프셋 값이 input 포함되지 않아도 되는 경우 반환 DateTimeOffset 된 개체에는 UTC 오프셋(+00:00)이 제공됩니다.
AllowInnerWhite input 요소에서 지정하지 않은 내부 공백을 포함할 수 있습니다 formats. 날짜 및 시간 구성 요소와 개별 구성 요소(오프셋 제외) 내에 추가 공백이 표시될 수 있으며 문자열을 구문 분석할 때 무시됩니다.
AllowLeadingWhite input 요소에서 지정하지 않은 선행 공백을 포함할 수 있습니다 formats. 문자열을 구문 분석할 때 무시됩니다.
AllowTrailingWhite input 요소에서 지정하지 않은 후행 공백을 포함할 수 있습니다 formats. 문자열을 구문 분석할 때 무시됩니다.
AllowWhiteSpaces 의 요소에서 input 지정하지 않은 선행, 후행 및 내부 공백을 포함할 수 있습니다 formats. 문자열을 구문 분석할 때 일치하는 요소 formats 에 지정되지 않은 추가 공백 문자는 모두 무시됩니다.
None 에서 추가 공백이 허용되지 않음을 input나타냅니다. 일치가 성공하려면 의 특정 요소 formats 에 지정된 대로 공백이 정확하게 나타나야 합니다. 기본 동작입니다.
RoundtripKind 구조체에 속성이 DateTimeOffset 포함되어 Kind 있지 않으므로 아무런 효과가 없습니다.

적용 대상

TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset)

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 날짜 및 시간의 지정된 문자열 표현을 해당 DateTimeOffset으로 변환합니다. 문자열 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

public:
 static bool TryParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParseExact (string input, string format, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParseExact (string? input, string? format, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParseExact : string * string * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParseExact (input As String, format As String, formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

매개 변수

input
String

변환할 날짜 및 시간이 포함된 문자열입니다.

format
String

input의 필수 형식을 정의하는 서식 지정자입니다.

formatProvider
IFormatProvider

input에 대한 문화권별 형식 지정 정보를 제공하는 개체입니다.

styles
DateTimeStyles

input에 사용할 수 있는 형식을 나타내는 열거형 값의 비트 조합입니다. 지정할 일반적인 값은 None입니다.

result
DateTimeOffset

메서드가 반환될 때 변환에 성공한 경우 의 input날짜 및 시간에 해당하는 를 포함하거나 변환에 실패한 경우 DateTimeOffset.MinValue를 포함합니다DateTimeOffset. input 매개 변수가 null이거나 formatprovider에 정의된 예상 형식에 따라 날짜와 시간을 나타내는 유효한 문자열 표현을 포함하지 않으면 변환이 실패합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

반환

input 매개 변수가 변환되었으면 true이고, 변환되지 않았으면 false입니다.

예외

styles에 정의되지 않은 DateTimeStyles 값이 들어 있습니다.

또는

NoCurrentDateDefault은 지원되지 않습니다.

또는

styles에 함께 사용할 수 없는 DateTimeStyles 값이 들어 있습니다.

예제

다음 예제에서는 표준 및 사용자 지정 형식 지정자, 고정 문화권 및 다양한 DateTimeStyles 값을 사용하여 TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset) 여러 날짜 및 시간 문자열을 구문 분석합니다.

string dateString, format;
DateTimeOffset result;
IFormatProvider provider = CultureInfo.InvariantCulture;

// Parse date-only value with invariant culture and assume time is UTC.
dateString = "06/15/2008";
format = "d";
if (DateTimeOffset.TryParseExact(dateString, format, provider,
                                 DateTimeStyles.AssumeUniversal,
                                 out result))
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
else
   Console.WriteLine("'{0}' is not in the correct format.", dateString);

// Parse date-only value with leading white space.
// Should return False because only trailing white space is
// specified in method call.
dateString = " 06/15/2008";
if (DateTimeOffset.TryParseExact(dateString, format, provider,
                                 DateTimeStyles.AllowTrailingWhite,
                                 out result))
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
else
   Console.WriteLine("'{0}' is not in the correct format.", dateString);

// Parse date and time value, and allow all white space.
dateString = " 06/15/   2008  15:15    -05:00";
format = "MM/dd/yyyy H:mm zzz";
if (DateTimeOffset.TryParseExact(dateString, format, provider,
                                 DateTimeStyles.AllowWhiteSpaces,
                                 out result))
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
else
   Console.WriteLine("'{0}' is not in the correct format.", dateString);

// Parse date and time and convert to UTC.
dateString = "  06/15/2008 15:15:30 -05:00";
format = "MM/dd/yyyy H:mm:ss zzz";
if (DateTimeOffset.TryParseExact(dateString, format, provider,
                                DateTimeStyles.AllowWhiteSpaces |
                                DateTimeStyles.AdjustToUniversal,
                                out result))
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
else
   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 +00:00.
//    ' 06/15/2008' is not in the correct format.
//    ' 06/15/   2008  15:15    -05:00' converts to 6/15/2008 3:15:00 PM -05:00.
//    '  06/15/2008 15:15:30 -05:00' converts to 6/15/2008 8:15:30 PM +00:00.
let provider = CultureInfo.InvariantCulture

// Parse date-only value with invariant culture and assume time is UTC.
let dateString = "06/15/2008"
let format = "d"
match DateTimeOffset.TryParseExact(dateString, format, provider, DateTimeStyles.AssumeUniversal) with
| true, result ->
    printfn $"'{dateString}' converts to {result}."
| _ ->
    printfn $"'{dateString}' is not in the correct format."

// Parse date-only value with leading white space.
// Should return False because only trailing white space is
// specified in method call.
let dateString = " 06/15/2008"
match DateTimeOffset.TryParseExact(dateString, format, provider, DateTimeStyles.AllowTrailingWhite) with
| true, result ->
    printfn $"'{dateString}' converts to {result}."
| _ ->
    printfn $"'{dateString}' is not in the correct format."

// Parse date and time value, and allow all white space.
let dateString = " 06/15/   2008  15:15    -05:00"
let format = "MM/dd/yyyy H:mm zzz"
match DateTimeOffset.TryParseExact(dateString, format, provider, DateTimeStyles.AllowWhiteSpaces) with
| true, result ->
    printfn $"'{dateString}' converts to {result}."
| _ ->
    printfn $"'{dateString}' is not in the correct format."

// Parse date and time and convert to UTC.
let dateString = "  06/15/2008 15:15:30 -05:00"
let format = "MM/dd/yyyy H:mm:ss zzz"
match DateTimeOffset.TryParseExact(dateString, format, provider, DateTimeStyles.AllowWhiteSpaces ||| DateTimeStyles.AdjustToUniversal) with
| true, result ->
    printfn $"'{dateString}' converts to {result}."
| _ ->
    printfn $"'{dateString}' is not in the correct format."

// The example displays the following output:
//    '06/15/2008' converts to 6/15/2008 12:00:00 AM +00:00.
//    ' 06/15/2008' is not in the correct format.
//    ' 06/15/   2008  15:15    -05:00' converts to 6/15/2008 3:15:00 PM -05:00.
//    '  06/15/2008 15:15:30 -05:00' converts to 6/15/2008 8:15:30 PM +00:00.
Dim dateString, format As String  
Dim result As DateTimeOffset
Dim provider As CultureInfo = CultureInfo.InvariantCulture

' Parse date-only value with invariant culture and assume time is UTC.
dateString = "06/15/2008"
format = "d"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                   DateTimeStyles.AssumeUniversal, _
                                   result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If 

' Parse date-only value with leading white space.
' Should return False because only trailing white space is  
' specified in method call.
dateString = " 06/15/2008"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowTrailingWhite, _
                                result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If 

' Parse date and time value, and allow all white space.
dateString = " 06/15/   2008  15:15    -05:00"
format = "MM/dd/yyyy H:mm zzz"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowWhiteSpaces, _
                                result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If 

' Parse date and time and convert to UTC.
dateString = "  06/15/2008 15:15:30 -05:00"   
format = "MM/dd/yyyy H:mm:ss zzz"       
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowWhiteSpaces Or _
                                DateTimeStyles.AdjustToUniversal, _
                                result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If 
' The example displays the following output:
'    '06/15/2008' converts to 6/15/2008 12:00:00 AM +00:00.
'    ' 06/15/2008' is not in the correct format.
'    ' 06/15/   2008  15:15    -05:00' converts to 6/15/2008 3:15:00 PM -05:00.
'    '  06/15/2008 15:15:30 -05:00' converts to 6/15/2008 8:15:30 PM +00:00.

다음 예제에서는 다양한 DateTimeStyles 값을 사용하여 ISO 8601을 준수해야 하는 문자열 배열을 구문 분석합니다. 예제의 출력과 같이 적절한 형식의 문자열은 다음과 같은 경우 구문 분석하지 못합니다.

UTC 오프셋을 지정하지 않는 문자열은 메서드 호출에 플래그가 제공되지 않는 한 DateTimeStyles.AssumeUniversal 현지 표준 시간대의 오프셋(이 경우 태평양 일광 표준 시간대의 오프셋을 반영하는 -07:00)을 갖는 것으로 간주됩니다. 이 경우 유니버설 조정 시간으로 간주됩니다.

open System
open System.Globalization

let parseWithISO8601 dateStrings styles =
    printfn $"Parsing with {styles}:"
    for dateString in dateStrings do
        match DateTimeOffset.TryParseExact(dateString, "O", null, styles) with
        | true, date ->
            printfn $"""   {dateString,-35} --> {date.ToString "yyyy-MM-dd HH:mm:ss.FF zzz"}"""
        | _ ->
            printfn $"   Unable to convert '{dateString}'"

let dateStrings = 
    [ "2018-08-18T12:45:16.0000000Z"
      "2018/08/18T12:45:16.0000000Z"
      "2018-18-08T12:45:16.0000000Z"
      "2018-08-18T12:45:16.0000000"
      " 2018-08-18T12:45:16.0000000Z "
      "2018-08-18T12:45:16.0000000+02:00"
      "2018-08-18T12:45:16.0000000-07:00" ]

parseWithISO8601 dateStrings DateTimeStyles.None
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AllowWhiteSpaces
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AdjustToUniversal
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AssumeLocal
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AssumeUniversal

// The example displays the following output:
//      Parsing with None:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AllowWhiteSpaces:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//          2018-08-18T12:45:16.0000000Z       --> 2018-08-18 12:45:16 +00:00
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AdjustToUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 19:45:16 +00:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 10:45:16 +00:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 19:45:16 +00:00
//
//      -----
//
//      Parsing with AssumeLocal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AssumeUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 +00:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] dateStrings = { "2018-08-18T12:45:16.0000000Z",
                               "2018/08/18T12:45:16.0000000Z",
                               "2018-18-08T12:45:16.0000000Z",
                               "2018-08-18T12:45:16.0000000",                               
                               " 2018-08-18T12:45:16.0000000Z ",
                               "2018-08-18T12:45:16.0000000+02:00",
                               "2018-08-18T12:45:16.0000000-07:00" }; 
      
      ParseWithISO8601(dateStrings, DateTimeStyles.None);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AllowWhiteSpaces);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AdjustToUniversal);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeLocal);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeUniversal);   }

   private static void ParseWithISO8601(string[] dateStrings, DateTimeStyles styles)
   {   
      Console.WriteLine($"Parsing with {styles}:");
      DateTimeOffset date;
      foreach (var dateString in dateStrings)
      {
         if (DateTimeOffset.TryParseExact(dateString, "O", null, styles, out date))
         {
            Console.WriteLine($"   {dateString,-35} --> {date:yyyy-MM-dd HH:mm:ss.FF zzz}");
         }
         else
         {
            Console.WriteLine($"   Unable to convert '{dateString}'");
         }   
      } 
   }
}
// The example displays the following output:
//      Parsing with None:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AllowWhiteSpaces:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//          2018-08-18T12:45:16.0000000Z       --> 2018-08-18 12:45:16 +00:00
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AdjustToUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 19:45:16 +00:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 10:45:16 +00:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 19:45:16 +00:00
//
//      -----
//
//      Parsing with AssumeLocal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AssumeUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 +00:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
Imports System.Globalization

Public Module Example
   Public Sub Main()
      Dim dateStrings() = { "2018-08-18T12:45:16.0000000Z",
                            "2018/08/18T12:45:16.0000000Z",
                            "2018-18-08T12:45:16.0000000Z",
                            "2018-08-18T12:45:16.0000000",                               
                            " 2018-08-18T12:45:16.0000000Z ",
                            "2018-08-18T12:45:16.0000000+02:00",
                            "2018-08-18T12:45:16.0000000-07:00" } 
      
      ParseWithISO8601(dateStrings, DateTimeStyles.None)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AllowWhiteSpaces)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AdjustToUniversal)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeLocal)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeUniversal)   
   End Sub

   Private Sub ParseWithISO8601(dateStrings() As String, styles As DateTimeStyles)
      Console.WriteLine($"Parsing with {styles}:")
      Dim dat As DateTimeOffset
      For Each dateStr In dateStrings
         If DateTimeOffset.TryParseExact(dateStr, "O", Nothing, styles, dat) Then
            Console.WriteLine($"   {dateStr,-35} --> {dat:yyyy-MM-dd HH:mm:ss.FF zzz}")
         Else
            Console.WriteLine($"   Unable to convert '{dateStr}'")
         End If   
      Next 
   End Sub
End Module
' The example displays the following output:
'      Parsing with None:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
'         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
'
'      -----
'
'      Parsing with AllowWhiteSpaces:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
'          2018-08-18T12:45:16.0000000Z       --> 2018-08-18 12:45:16 +00:00
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
'
'      -----
'
'      Parsing with AdjustToUniversal:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 19:45:16 +00:00
'         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 10:45:16 +00:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 19:45:16 +00:00
'
'      -----
'
'      Parsing with AssumeLocal:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
'         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
'
'      -----
'
'      Parsing with AssumeUniversal:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 +00:00
'         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00

설명

변환이 TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset) 실패할 경우 이 메서드가 예외를 DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) throw하지 않는다는 점을 제외하고 메서드의 이 오버로드는 메서드와 같습니다. 매개 변수에 지정된 패턴과 정확히 일치해야 하는 날짜 및 시간의 문자열 표현을 format 구문 분석합니다. 문자열이 input 이 패턴과 일치하지 않는 경우 매개 변수로 정의된 공백의 몇 가지 가능한 변형이 styles 있으면 구문 분석 작업이 실패하고 메서드는 를 반환합니다 false.

매개 변수는 format 단일 표준 형식 지정자 또는 의 필수 패턴을 정의하는 하나 이상의 사용자 지정 형식 지정자를 포함하는 문자열입니다 input. 유효한 서식 코드에 대한 자세한 내용은 표준 날짜 및 시간 형식 문자열 및사용자 지정 날짜 및 시간 형식 문자열을 참조하세요. 에 오프셋이 z있어야 input함을 나타내는 , zz또는 zzz 사용자 지정 형식 지정자가 포함된 경우 format 해당 오프셋에는 음수 기호 또는 양수 기호가 포함되어야 합니다. 기호가 없으면 구문 분석 작업이 실패하고 메서드가 를 반환합니다 false.

에 날짜가 input 포함되지만 시간이 포함되어야 하는 경우 format 결과 DateTimeOffset 개체에 자정(0:00:00)의 시간이 할당됩니다. input 에 시간이 포함되지만 날짜가 없는 경우 format 결과 DateTimeOffset 개체에 로컬 시스템의 현재 날짜가 할당됩니다. 에 오프셋이 input 포함되지 않은 경우 format 결과 DateTimeOffset 개체의 오프셋은 매개 변수의 styles 값에 따라 달라집니다. 가 포함된 AssumeLocal경우 styles 로컬 표준 시간대의 오프셋이 개체에 DateTimeOffset 할당됩니다. 가 포함된 AssumeUniversal경우 styles UTC(협정 세계시) 오프셋 또는 +00:00이 개체에 DateTimeOffset 할당됩니다. 두 값을 모두 지정하지 않으면 현지 표준 시간대의 오프셋이 사용됩니다.

에 사용되는 input 특정 날짜 및 시간 기호와 문자열은 매개 변수로 formatProvider 정의됩니다. 가 표준 형식 지정자 문자열인 경우 formatinput 정확한 패턴도 마찬가지입니다. 매개 변수는 formatProvider 다음 중 하나일 수 있습니다.

가 이 nullCultureInfoformatprovider 현재 문화권에 해당하는 개체가 사용됩니다.

매개 변수는 styles 입력 문자열에 공백이 허용되는지 여부를 정의하고, 명시적 오프셋 구성 요소가 없는 문자열을 구문 분석하는 방법을 나타내며, 구문 분석 작업의 일부로 UTC 변환을 지원합니다. 를 제외한 NoCurrentDateDefault열거형의 DateTimeStyles 모든 멤버가 지원됩니다. 다음 표에서는 지원되는 각 멤버의 효과를 나열합니다.

DateTimeStyles 멤버 동작
AdjustToUniversal 및 를 input 구문 분석하고 필요한 경우 UTC로 변환합니다. 문자열을 구문 분석한 다음 반환 DateTimeOffset 된 개체의 메서드를 DateTimeOffset.ToUniversalTime 호출하는 것과 같습니다.
AssumeLocal format 에 오프셋 값이 input 포함되어 있지 않으면 반환 DateTimeOffset 된 개체에 현지 표준 시간대의 오프셋이 제공됩니다. 기본 동작입니다.
AssumeUniversal format 에 오프셋 값이 input 포함되어 있지 않으면 반환 DateTimeOffset 된 개체에 UTC 오프셋(+00:00)이 제공됩니다.
AllowInnerWhite 형식으로 input 지정되지 않은 내부 공백을 포함할 수 있습니다. 날짜 및 시간 구성 요소와 오프셋 이외의 개별 구성 요소 내에 추가 공백이 표시될 수 있으며 문자열을 구문 분석할 때 무시됩니다.
AllowLeadingWhite 에서 input 지정하지 않은 선행 공백을 format포함할 수 있습니다. 문자열을 구문 분석할 때 무시됩니다.
AllowTrailingWhite 에서 input 지정하지 않은 후행 공백을 format포함할 수 있습니다. 문자열을 구문 분석할 때 무시됩니다.
AllowWhiteSpaces 에서 input 지정하지 않은 선행, 후행 및 내부 공백을 format포함할 수 있습니다. 문자열을 구문 분석할 때 에 format 지정되지 않은 추가 공백 문자는 모두 무시됩니다.
None 에서 추가 공백이 허용되지 않음을 input나타냅니다. 공백은 에 format지정된 대로 정확하게 표시되어야 합니다. 기본 동작입니다.
RoundtripKind 구조체에 속성이 DateTimeOffset 포함되어 Kind 있지 않으므로 아무런 효과가 없습니다.

호출자 참고

.NET Framework 4 TryParseExact 에서는 구문 분석할 문자열에 시간 구성 요소와 규약이 없는 AM/PM 지정자가 포함되어 있으면 를 반환 false 합니다. .NET Framework 3.5 이전 버전에서는 AM/PM 지정자가 무시됩니다.

추가 정보

적용 대상