DateTimeFormatInfo.GetAllDateTimePatterns 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
날짜 및 시간 값의 형식을 지정할 수 있는 표준 패턴을 반환합니다.
오버로드
GetAllDateTimePatterns() |
날짜 및 시간 값의 형식을 지정할 수 있는 모든 표준 패턴을 반환합니다. |
GetAllDateTimePatterns(Char) |
지정된 표준 형식 문자열을 사용하여 날짜 및 시간 값의 형식을 지정할 수 있는 모든 패턴을 반환합니다. |
GetAllDateTimePatterns()
날짜 및 시간 값의 형식을 지정할 수 있는 모든 표준 패턴을 반환합니다.
public:
cli::array <System::String ^> ^ GetAllDateTimePatterns();
public string[] GetAllDateTimePatterns ();
member this.GetAllDateTimePatterns : unit -> string[]
Public Function GetAllDateTimePatterns () As String()
반환
- String[]
날짜 및 시간 값의 형식을 지정할 수 있는 표준 패턴이 포함된 배열입니다.
예제
다음 예제에서는 고정 문화권에 대 한 날짜 및 시간 서식 문자열과 특정 날짜의 형식을 지정 하는 데 해당 서식 문자열을 사용 하는 경우 생성 되는 결과 문자열을 표시 합니다.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
DateTime date = new DateTime(2014, 8, 28, 12, 28, 30);
DateTimeFormatInfo invDTF = new DateTimeFormatInfo();
String[] formats = invDTF.GetAllDateTimePatterns();
Console.WriteLine("{0,-40} {1}\n", "Pattern", "Result String");
foreach (var fmt in formats)
Console.WriteLine("{0,-40} {1}", fmt, date.ToString(fmt));
}
}
// The example displays the following output:
// Pattern Result String
//
// MM/dd/yyyy 08/28/2014
// yyyy-MM-dd 2014-08-28
// dddd, dd MMMM yyyy Thursday, 28 August 2014
// dddd, dd MMMM yyyy HH:mm Thursday, 28 August 2014 12:28
// dddd, dd MMMM yyyy hh:mm tt Thursday, 28 August 2014 12:28 PM
// dddd, dd MMMM yyyy H:mm Thursday, 28 August 2014 12:28
// dddd, dd MMMM yyyy h:mm tt Thursday, 28 August 2014 12:28 PM
// dddd, dd MMMM yyyy HH:mm:ss Thursday, 28 August 2014 12:28:30
// MM/dd/yyyy HH:mm 08/28/2014 12:28
// MM/dd/yyyy hh:mm tt 08/28/2014 12:28 PM
// MM/dd/yyyy H:mm 08/28/2014 12:28
// MM/dd/yyyy h:mm tt 08/28/2014 12:28 PM
// yyyy-MM-dd HH:mm 2014-08-28 12:28
// yyyy-MM-dd hh:mm tt 2014-08-28 12:28 PM
// yyyy-MM-dd H:mm 2014-08-28 12:28
// yyyy-MM-dd h:mm tt 2014-08-28 12:28 PM
// MM/dd/yyyy HH:mm:ss 08/28/2014 12:28:30
// yyyy-MM-dd HH:mm:ss 2014-08-28 12:28:30
// MMMM dd August 28
// MMMM dd August 28
// yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2014-08-28T12:28:30.0000000
// yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2014-08-28T12:28:30.0000000
// ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Thu, 28 Aug 2014 12:28:30 GMT
// ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Thu, 28 Aug 2014 12:28:30 GMT
// yyyy'-'MM'-'dd'T'HH':'mm':'ss 2014-08-28T12:28:30
// HH:mm 12:28
// hh:mm tt 12:28 PM
// H:mm 12:28
// h:mm tt 12:28 PM
// HH:mm:ss 12:28:30
// yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2014-08-28 12:28:30Z
// dddd, dd MMMM yyyy HH:mm:ss Thursday, 28 August 2014 12:28:30
// yyyy MMMM 2014 August
// yyyy MMMM 2014 August
Imports System.Globalization
Module Example
Public Sub Main()
Dim date1 As Date = #08/28/2014 12:28:30PM#
Dim invDTF As New DateTimeFormatInfo()
Dim formats() As String = invDTF.GetAllDateTimePatterns()
Console.WriteLine("{0,-40} {1}", "Pattern", "Result String")
Console.WriteLine()
For Each fmt In formats
Console.WriteLine("{0,-40} {1}", fmt, date1.ToString(fmt))
Next
End Sub
End Module
' The example displays the following output:
' Pattern Result String
'
' MM/dd/yyyy 08/28/2014
' yyyy-MM-dd 2014-08-28
' dddd, dd MMMM yyyy Thursday, 28 August 2014
' dddd, dd MMMM yyyy HH:mm Thursday, 28 August 2014 12:28
' dddd, dd MMMM yyyy hh:mm tt Thursday, 28 August 2014 12:28 PM
' dddd, dd MMMM yyyy H:mm Thursday, 28 August 2014 12:28
' dddd, dd MMMM yyyy h:mm tt Thursday, 28 August 2014 12:28 PM
' dddd, dd MMMM yyyy HH:mm:ss Thursday, 28 August 2014 12:28:30
' MM/dd/yyyy HH:mm 08/28/2014 12:28
' MM/dd/yyyy hh:mm tt 08/28/2014 12:28 PM
' MM/dd/yyyy H:mm 08/28/2014 12:28
' MM/dd/yyyy h:mm tt 08/28/2014 12:28 PM
' yyyy-MM-dd HH:mm 2014-08-28 12:28
' yyyy-MM-dd hh:mm tt 2014-08-28 12:28 PM
' yyyy-MM-dd H:mm 2014-08-28 12:28
' yyyy-MM-dd h:mm tt 2014-08-28 12:28 PM
' MM/dd/yyyy HH:mm:ss 08/28/2014 12:28:30
' yyyy-MM-dd HH:mm:ss 2014-08-28 12:28:30
' MMMM dd August 28
' MMMM dd August 28
' yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2014-08-28T12:28:30.0000000
' yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2014-08-28T12:28:30.0000000
' ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Thu, 28 Aug 2014 12:28:30 GMT
' ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Thu, 28 Aug 2014 12:28:30 GMT
' yyyy'-'MM'-'dd'T'HH':'mm':'ss 2014-08-28T12:28:30
' HH:mm 12:28
' hh:mm tt 12:28 PM
' H:mm 12:28
' h:mm tt 12:28 PM
' HH:mm:ss 12:28:30
' yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2014-08-28 12:28:30Z
' dddd, dd MMMM yyyy HH:mm:ss Thursday, 28 August 2014 12:28:30
' yyyy MMMM 2014 August
' yyyy MMMM 2014 August
이 예제에서는 DateTimeFormatInfo 생성자를 호출 하 여 고정 문화권을 나타내는 개체를 인스턴스화합니다 DateTimeFormatInfo . DateTimeFormatInfo속성에서 고정 문화권을 나타내는를 검색할 수도 있습니다 InvariantInfo .
설명
이 메서드는 표준 서식 문자열에 해당 하는 모든 사용자 지정 서식 문자열을 포함 하는 배열을 반환 합니다. 표준 형식 문자열 목록은 표준 날짜 및 시간 형식 문자열 을 참조 하세요.
서식 지정 작업에서 메서드에 의해 반환 된 배열에 사용자 지정 형식 문자열을 사용할 수 있습니다 GetAllDateTimePatterns . 그러나 이렇게 하면 및 메서드에서 해당 형식 지정 작업에서 반환 된 날짜 및 시간 값의 문자열 표현을 항상 성공적으로 구문 분석할 수 없습니다 Parse
TryParse
. 따라서 메서드에서 반환 된 사용자 지정 형식 문자열을 GetAllDateTimePatterns 사용 하 여 날짜 및 시간 값을 라운드트립 하는 것으로 간주할 수 없습니다. 다음 예제에서는 이 문제를 보여 줍니다. DateTimeFormatInfo이탈리아 (이탈리아어) 문화권에 대 한 형식 정보를 포함 하는 개체를 검색 합니다. 메서드에서 반환 된 배열의 각 사용자 지정 서식 문자열을 메서드에 전달 GetAllDateTimePatterns() DateTime.ToString(String) 하 여 날짜 및 시간의 문자열 표현을 만듭니다. 그런 다음이 예제에서는 메서드를 호출 하 여이 값의 구문 분석을 시도 DateTime.TryParse(String, DateTime) 합니다. 예제의 출력에 표시 된 것 처럼 일부 사용자 지정 형식 문자열은 라운드트립 하는 날짜 및 시간 값을 생성 하지 않습니다.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo culture = CultureInfo.CreateSpecificCulture("it-IT");
DateTime date1 = new DateTime(2011, 02, 01, 7, 30, 45, 0);
DateTime date2;
int total = 0;
int noRoundTrip = 0;
foreach (var fmt in culture.DateTimeFormat.GetAllDateTimePatterns()) {
total += 1;
if (! DateTime.TryParse(date1.ToString(fmt), out date2)) {
noRoundTrip++;
Console.WriteLine("Unable to parse {0:" + fmt + "} (format '{1}')",
date1, fmt);
}
}
Console.WriteLine("\nUnable to round-trip {0} of {1} format strings.",
noRoundTrip, total);
}
}
// The example displays the following output:
// Unable to parse Tuesday 1 February 2011 7.30 (format 'dddd d MMMM yyyy H.mm')
// Unable to parse Tuesday 1 February 2011 07.30 (format 'dddd d MMMM yyyy HH.mm')
// Unable to parse 1-Feb-11 7.30 (format 'd-MMM-yy H.mm')
// Unable to parse 1-Feb-11 07.30 (format 'd-MMM-yy HH.mm')
// Unable to parse 1 February 2011 7.30 (format 'd MMMM yyyy H.mm')
// Unable to parse 1 February 2011 07.30 (format 'd MMMM yyyy HH.mm')
// Unable to parse Tuesday 1 February 2011 7.30.45 (format 'dddd d MMMM yyyy H.mm.ss')
// Unable to parse Tuesday 1 February 2011 07.30.45 (format 'dddd d MMMM yyyy HH.mm.ss')
// Unable to parse 1-Feb-11 7.30.45 (format 'd-MMM-yy H.mm.ss')
// Unable to parse 1-Feb-11 07.30.45 (format 'd-MMM-yy HH.mm.ss')
// Unable to parse 1 February 2011 7.30.45 (format 'd MMMM yyyy H.mm.ss')
// Unable to parse 1 February 2011 07.30.45 (format 'd MMMM yyyy HH.mm.ss')
// Unable to parse 01/02/2011 7.30 (format 'dd/MM/yyyy H.mm')
// Unable to parse 01/02/2011 07.30 (format 'dd/MM/yyyy HH.mm')
// Unable to parse 01/Feb/2011 7.30 (format 'dd/MMM/yyyy H.mm')
// Unable to parse 01/Feb/2011 07.30 (format 'dd/MMM/yyyy HH.mm')
// Unable to parse 01/02/11 7.30 (format 'dd/MM/yy H.mm')
// Unable to parse 01/02/11 07.30 (format 'dd/MM/yy HH.mm')
// Unable to parse 01.2.11 7.30 (format 'dd.M.yy H.mm')
// Unable to parse 01.2.11 07.30 (format 'dd.M.yy HH.mm')
// Unable to parse 1/2/11 7.30 (format 'd/M/yy H.mm')
// Unable to parse 1/2/11 07.30 (format 'd/M/yy HH.mm')
// Unable to parse 2011-02-01 7.30 (format 'yyyy-MM-dd H.mm')
// Unable to parse 2011-02-01 07.30 (format 'yyyy-MM-dd HH.mm')
// Unable to parse 01/02/2011 7.30.45 (format 'dd/MM/yyyy H.mm.ss')
// Unable to parse 01/02/2011 07.30.45 (format 'dd/MM/yyyy HH.mm.ss')
// Unable to parse 01/Feb/2011 7.30.45 (format 'dd/MMM/yyyy H.mm.ss')
// Unable to parse 01/Feb/2011 07.30.45 (format 'dd/MMM/yyyy HH.mm.ss')
// Unable to parse 01/02/11 7.30.45 (format 'dd/MM/yy H.mm.ss')
// Unable to parse 01/02/11 07.30.45 (format 'dd/MM/yy HH.mm.ss')
// Unable to parse 01.2.11 7.30.45 (format 'dd.M.yy H.mm.ss')
// Unable to parse 01.2.11 07.30.45 (format 'dd.M.yy HH.mm.ss')
// Unable to parse 1/2/11 7.30.45 (format 'd/M/yy H.mm.ss')
// Unable to parse 1/2/11 07.30.45 (format 'd/M/yy HH.mm.ss')
// Unable to parse 2011-02-01 7.30.45 (format 'yyyy-MM-dd H.mm.ss')
// Unable to parse 2011-02-01 07.30.45 (format 'yyyy-MM-dd HH.mm.ss')
// Unable to parse Tuesday 1 February 2011 7.30.45 (format 'dddd d MMMM yyyy H.mm.ss')
// Unable to parse Tuesday 1 February 2011 07.30.45 (format 'dddd d MMMM yyyy HH.mm.ss')
// Unable to parse 1-Feb-11 7.30.45 (format 'd-MMM-yy H.mm.ss')
// Unable to parse 1-Feb-11 07.30.45 (format 'd-MMM-yy HH.mm.ss')
// Unable to parse 1 February 2011 7.30.45 (format 'd MMMM yyyy H.mm.ss')
// Unable to parse 1 February 2011 07.30.45 (format 'd MMMM yyyy HH.mm.ss')
//
// Unable to round-trip 42 of 98 format strings.
Imports System.Globalization
Module Example
Public Sub Main()
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("it-IT")
Dim date1 = New DateTime(2011, 02, 01, 7, 30, 45, 0)
Dim date2 As DateTime
Dim total, noRoundTrip As Integer
For Each fmt In culture.DateTimeFormat.GetAllDateTimePatterns()
total += 1
If Not DateTime.TryParse(date1.ToString(fmt), date2)
noRoundTrip += 1
Console.WriteLine("Unable to parse {0:" + fmt + "} (format '{1}')",
date1, fmt)
End If
Next
Console.WriteLine()
Console.WriteLine("Unable to round-trip {0} of {1} format strings.",
noRoundTrip, total)
End Sub
End Module
' The example displays the following output:
' Unable to parse Tuesday 1 February 2011 7.30 (format 'dddd d MMMM yyyy H.mm')
' Unable to parse Tuesday 1 February 2011 07.30 (format 'dddd d MMMM yyyy HH.mm')
' Unable to parse 1-Feb-11 7.30 (format 'd-MMM-yy H.mm')
' Unable to parse 1-Feb-11 07.30 (format 'd-MMM-yy HH.mm')
' Unable to parse 1 February 2011 7.30 (format 'd MMMM yyyy H.mm')
' Unable to parse 1 February 2011 07.30 (format 'd MMMM yyyy HH.mm')
' Unable to parse Tuesday 1 February 2011 7.30.45 (format 'dddd d MMMM yyyy H.mm.ss')
' Unable to parse Tuesday 1 February 2011 07.30.45 (format 'dddd d MMMM yyyy HH.mm.ss')
' Unable to parse 1-Feb-11 7.30.45 (format 'd-MMM-yy H.mm.ss')
' Unable to parse 1-Feb-11 07.30.45 (format 'd-MMM-yy HH.mm.ss')
' Unable to parse 1 February 2011 7.30.45 (format 'd MMMM yyyy H.mm.ss')
' Unable to parse 1 February 2011 07.30.45 (format 'd MMMM yyyy HH.mm.ss')
' Unable to parse 01/02/2011 7.30 (format 'dd/MM/yyyy H.mm')
' Unable to parse 01/02/2011 07.30 (format 'dd/MM/yyyy HH.mm')
' Unable to parse 01/Feb/2011 7.30 (format 'dd/MMM/yyyy H.mm')
' Unable to parse 01/Feb/2011 07.30 (format 'dd/MMM/yyyy HH.mm')
' Unable to parse 01/02/11 7.30 (format 'dd/MM/yy H.mm')
' Unable to parse 01/02/11 07.30 (format 'dd/MM/yy HH.mm')
' Unable to parse 01.2.11 7.30 (format 'dd.M.yy H.mm')
' Unable to parse 01.2.11 07.30 (format 'dd.M.yy HH.mm')
' Unable to parse 1/2/11 7.30 (format 'd/M/yy H.mm')
' Unable to parse 1/2/11 07.30 (format 'd/M/yy HH.mm')
' Unable to parse 2011-02-01 7.30 (format 'yyyy-MM-dd H.mm')
' Unable to parse 2011-02-01 07.30 (format 'yyyy-MM-dd HH.mm')
' Unable to parse 01/02/2011 7.30.45 (format 'dd/MM/yyyy H.mm.ss')
' Unable to parse 01/02/2011 07.30.45 (format 'dd/MM/yyyy HH.mm.ss')
' Unable to parse 01/Feb/2011 7.30.45 (format 'dd/MMM/yyyy H.mm.ss')
' Unable to parse 01/Feb/2011 07.30.45 (format 'dd/MMM/yyyy HH.mm.ss')
' Unable to parse 01/02/11 7.30.45 (format 'dd/MM/yy H.mm.ss')
' Unable to parse 01/02/11 07.30.45 (format 'dd/MM/yy HH.mm.ss')
' Unable to parse 01.2.11 7.30.45 (format 'dd.M.yy H.mm.ss')
' Unable to parse 01.2.11 07.30.45 (format 'dd.M.yy HH.mm.ss')
' Unable to parse 1/2/11 7.30.45 (format 'd/M/yy H.mm.ss')
' Unable to parse 1/2/11 07.30.45 (format 'd/M/yy HH.mm.ss')
' Unable to parse 2011-02-01 7.30.45 (format 'yyyy-MM-dd H.mm.ss')
' Unable to parse 2011-02-01 07.30.45 (format 'yyyy-MM-dd HH.mm.ss')
' Unable to parse Tuesday 1 February 2011 7.30.45 (format 'dddd d MMMM yyyy H.mm.ss')
' Unable to parse Tuesday 1 February 2011 07.30.45 (format 'dddd d MMMM yyyy HH.mm.ss')
' Unable to parse 1-Feb-11 7.30.45 (format 'd-MMM-yy H.mm.ss')
' Unable to parse 1-Feb-11 07.30.45 (format 'd-MMM-yy HH.mm.ss')
' Unable to parse 1 February 2011 7.30.45 (format 'd MMMM yyyy H.mm.ss')
' Unable to parse 1 February 2011 07.30.45 (format 'd MMMM yyyy HH.mm.ss')
'
' Unable to round-trip 42 of 98 format strings.
미리 정의 된 여러 사용자 지정 형식으로 표현할 수 있는 날짜 및 시간의 문자열 표현을 구문 분석 하려면 다음 메서드 중 하나를 호출 합니다.
DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles)
DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles)
DateTime.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime)
DateTimeOffset.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset)
메서드에서 반환 된 배열의 사용자 지정 형식 문자열은 GetAllDateTimePatterns DateTimeFormatInfo 개체가 서식 지정 정보를 제공 하는 문화권의 현재 달력에 따라 달라 집니다. 일정이 변경 되는 경우이 메서드에서 반환 하는 배열도 변경 됩니다.
추가 정보
적용 대상
GetAllDateTimePatterns(Char)
지정된 표준 형식 문자열을 사용하여 날짜 및 시간 값의 형식을 지정할 수 있는 모든 패턴을 반환합니다.
public:
cli::array <System::String ^> ^ GetAllDateTimePatterns(char format);
public string[] GetAllDateTimePatterns (char format);
member this.GetAllDateTimePatterns : char -> string[]
Public Function GetAllDateTimePatterns (format As Char) As String()
매개 변수
- format
- Char
표준 서식 문자열입니다.
반환
- String[]
지정된 형식 문자열을 사용하여 날짜 및 시간의 형식을 지정할 수 있는 표준 패턴이 들어 있는 배열입니다.
예외
format
이 유효한 형식 문자열이 아닙니다.
예제
다음 예에서는 현재 달력에 대 한 날짜 및 시간 패턴을 표시 합니다.
#using <System.dll>
using namespace System;
using namespace System::Globalization;
public ref class SamplesDateTimeFormatInfo
{
public:
static void Main()
{
// Creates a new DateTimeFormatinfo.
DateTimeFormatInfo^ myDtfi = gcnew DateTimeFormatInfo;
// Gets and prints all the patterns
array<String^>^myPatternsArray = myDtfi->GetAllDateTimePatterns();
Console::WriteLine( "ALL the patterns:" );
PrintIndexAndValues( myPatternsArray );
// Gets and prints the pattern(s) associated with some of the format characters.
myPatternsArray = myDtfi->GetAllDateTimePatterns( 'd' );
Console::WriteLine( "The patterns for 'd':" );
PrintIndexAndValues( myPatternsArray );
myPatternsArray = myDtfi->GetAllDateTimePatterns( 'D' );
Console::WriteLine( "The patterns for 'D':" );
PrintIndexAndValues( myPatternsArray );
myPatternsArray = myDtfi->GetAllDateTimePatterns( 'f' );
Console::WriteLine( "The patterns for 'f':" );
PrintIndexAndValues( myPatternsArray );
myPatternsArray = myDtfi->GetAllDateTimePatterns( 'F' );
Console::WriteLine( "The patterns for 'F':" );
PrintIndexAndValues( myPatternsArray );
myPatternsArray = myDtfi->GetAllDateTimePatterns( 'r' );
Console::WriteLine( "The patterns for 'r':" );
PrintIndexAndValues( myPatternsArray );
myPatternsArray = myDtfi->GetAllDateTimePatterns( 'R' );
Console::WriteLine( "The patterns for 'R':" );
PrintIndexAndValues( myPatternsArray );
}
public:
static void PrintIndexAndValues( array<String^>^myArray ) {
int i = 0;
for each ( String^ s in myArray )
Console::WriteLine( "\t[{0}]:\t{1}", i++, s );
Console::WriteLine();
}
};
int main()
{
SamplesDateTimeFormatInfo::Main();
}
/*
This code produces the following output.
ALL the patterns:
[0]: MM/dd/yyyy
[1]: dddd, dd MMMM yyyy
[2]: dddd, dd MMMM yyyy HH:mm
[3]: dddd, dd MMMM yyyy hh:mm tt
[4]: dddd, dd MMMM yyyy H:mm
[5]: dddd, dd MMMM yyyy h:mm tt
[6]: dddd, dd MMMM yyyy HH:mm:ss
[7]: MM/dd/yyyy HH:mm
[8]: MM/dd/yyyy hh:mm tt
[9]: MM/dd/yyyy H:mm
[10]: MM/dd/yyyy h:mm tt
[11]: MM/dd/yyyy HH:mm:ss
[12]: MMMM dd
[13]: MMMM dd
[14]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
[15]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
[16]: yyyy'-'MM'-'dd'T'HH':'mm':'ss
[17]: HH:mm
[18]: hh:mm tt
[19]: H:mm
[20]: h:mm tt
[21]: HH:mm:ss
[22]: yyyy'-'MM'-'dd HH':'mm':'ss'Z'
[23]: dddd, dd MMMM yyyy HH:mm:ss
[24]: yyyy MMMM
[25]: yyyy MMMM
The patterns for 'd':
[0]: MM/dd/yyyy
The patterns for 'D':
[0]: dddd, dd MMMM yyyy
The patterns for 'f':
[0]: dddd, dd MMMM yyyy HH:mm
[1]: dddd, dd MMMM yyyy hh:mm tt
[2]: dddd, dd MMMM yyyy H:mm
[3]: dddd, dd MMMM yyyy h:mm tt
The patterns for 'F':
[0]: dddd, dd MMMM yyyy HH:mm:ss
The patterns for 'r':
[0]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
The patterns for 'R':
[0]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
*/
using System;
using System.Globalization;
public class SamplesDateTimeFormatInfo {
public static void Main() {
// Creates a new DateTimeFormatinfo.
DateTimeFormatInfo myDtfi = new DateTimeFormatInfo();
// Gets and prints all the patterns.
String[] myPatternsArray = myDtfi.GetAllDateTimePatterns();
Console.WriteLine( "ALL the patterns:" );
PrintIndexAndValues( myPatternsArray );
// Gets and prints the pattern(s) associated with some of the format characters.
myPatternsArray = myDtfi.GetAllDateTimePatterns('d');
Console.WriteLine( "The patterns for 'd':" );
PrintIndexAndValues( myPatternsArray );
myPatternsArray = myDtfi.GetAllDateTimePatterns('D');
Console.WriteLine( "The patterns for 'D':" );
PrintIndexAndValues( myPatternsArray );
myPatternsArray = myDtfi.GetAllDateTimePatterns('f');
Console.WriteLine( "The patterns for 'f':" );
PrintIndexAndValues( myPatternsArray );
myPatternsArray = myDtfi.GetAllDateTimePatterns('F');
Console.WriteLine( "The patterns for 'F':" );
PrintIndexAndValues( myPatternsArray );
myPatternsArray = myDtfi.GetAllDateTimePatterns('r');
Console.WriteLine( "The patterns for 'r':" );
PrintIndexAndValues( myPatternsArray );
myPatternsArray = myDtfi.GetAllDateTimePatterns('R');
Console.WriteLine( "The patterns for 'R':" );
PrintIndexAndValues( myPatternsArray );
}
public static void PrintIndexAndValues( String[] myArray ) {
int i = 0;
foreach ( string s in myArray )
Console.WriteLine( "\t[{0}]:\t{1}", i++, s );
Console.WriteLine();
}
}
/*
This code produces the following output.
ALL the patterns:
[0]: MM/dd/yyyy
[1]: dddd, dd MMMM yyyy
[2]: dddd, dd MMMM yyyy HH:mm
[3]: dddd, dd MMMM yyyy hh:mm tt
[4]: dddd, dd MMMM yyyy H:mm
[5]: dddd, dd MMMM yyyy h:mm tt
[6]: dddd, dd MMMM yyyy HH:mm:ss
[7]: MM/dd/yyyy HH:mm
[8]: MM/dd/yyyy hh:mm tt
[9]: MM/dd/yyyy H:mm
[10]: MM/dd/yyyy h:mm tt
[11]: MM/dd/yyyy HH:mm:ss
[12]: MMMM dd
[13]: MMMM dd
[14]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
[15]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
[16]: yyyy'-'MM'-'dd'T'HH':'mm':'ss
[17]: HH:mm
[18]: hh:mm tt
[19]: H:mm
[20]: h:mm tt
[21]: HH:mm:ss
[22]: yyyy'-'MM'-'dd HH':'mm':'ss'Z'
[23]: dddd, dd MMMM yyyy HH:mm:ss
[24]: yyyy MMMM
[25]: yyyy MMMM
The patterns for 'd':
[0]: MM/dd/yyyy
The patterns for 'D':
[0]: dddd, dd MMMM yyyy
The patterns for 'f':
[0]: dddd, dd MMMM yyyy HH:mm
[1]: dddd, dd MMMM yyyy hh:mm tt
[2]: dddd, dd MMMM yyyy H:mm
[3]: dddd, dd MMMM yyyy h:mm tt
The patterns for 'F':
[0]: dddd, dd MMMM yyyy HH:mm:ss
The patterns for 'r':
[0]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
The patterns for 'R':
[0]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
*/
Imports System.Globalization
Public Class SamplesDateTimeFormatInfo
Public Shared Sub Main()
' Creates a new DateTimeFormatinfo.
Dim myDtfi As New DateTimeFormatInfo()
' Gets and prints all the patterns.
Dim myPatternsArray As String() = myDtfi.GetAllDateTimePatterns()
Console.WriteLine("ALL the patterns:")
PrintIndexAndValues(myPatternsArray)
' Gets and prints the pattern(s) associated with some of the format characters.
myPatternsArray = myDtfi.GetAllDateTimePatterns("d"c)
Console.WriteLine("The patterns for 'd':")
PrintIndexAndValues(myPatternsArray)
myPatternsArray = myDtfi.GetAllDateTimePatterns("D"c)
Console.WriteLine("The patterns for 'D':")
PrintIndexAndValues(myPatternsArray)
myPatternsArray = myDtfi.GetAllDateTimePatterns("f"c)
Console.WriteLine("The patterns for 'f':")
PrintIndexAndValues(myPatternsArray)
myPatternsArray = myDtfi.GetAllDateTimePatterns("F"c)
Console.WriteLine("The patterns for 'F':")
PrintIndexAndValues(myPatternsArray)
myPatternsArray = myDtfi.GetAllDateTimePatterns("r"c)
Console.WriteLine("The patterns for 'r':")
PrintIndexAndValues(myPatternsArray)
myPatternsArray = myDtfi.GetAllDateTimePatterns("R"c)
Console.WriteLine("The patterns for 'R':")
PrintIndexAndValues(myPatternsArray)
End Sub
Public Shared Sub PrintIndexAndValues(myArray() As String)
Dim i As Integer = 0
Dim s As String
For Each s In myArray
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
+ "{1}", i, s)
i += 1
Next s
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' ALL the patterns:
' [0]: MM/dd/yyyy
' [1]: dddd, dd MMMM yyyy
' [2]: dddd, dd MMMM yyyy HH:mm
' [3]: dddd, dd MMMM yyyy hh:mm tt
' [4]: dddd, dd MMMM yyyy H:mm
' [5]: dddd, dd MMMM yyyy h:mm tt
' [6]: dddd, dd MMMM yyyy HH:mm:ss
' [7]: MM/dd/yyyy HH:mm
' [8]: MM/dd/yyyy hh:mm tt
' [9]: MM/dd/yyyy H:mm
' [10]: MM/dd/yyyy h:mm tt
' [11]: MM/dd/yyyy HH:mm:ss
' [12]: MMMM dd
' [13]: MMMM dd
' [14]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
' [15]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
' [16]: yyyy'-'MM'-'dd'T'HH':'mm':'ss
' [17]: HH:mm
' [18]: hh:mm tt
' [19]: H:mm
' [20]: h:mm tt
' [21]: HH:mm:ss
' [22]: yyyy'-'MM'-'dd HH':'mm':'ss'Z'
' [23]: dddd, dd MMMM yyyy HH:mm:ss
' [24]: yyyy MMMM
' [25]: yyyy MMMM
'
' The patterns for 'd':
' [0]: MM/dd/yyyy
'
' The patterns for 'D':
' [0]: dddd, dd MMMM yyyy
'
' The patterns for 'f':
' [0]: dddd, dd MMMM yyyy HH:mm
' [1]: dddd, dd MMMM yyyy hh:mm tt
' [2]: dddd, dd MMMM yyyy H:mm
' [3]: dddd, dd MMMM yyyy h:mm tt
'
' The patterns for 'F':
' [0]: dddd, dd MMMM yyyy HH:mm:ss
'
' The patterns for 'r':
' [0]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
'
' The patterns for 'R':
' [0]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
'
설명
이 메서드는 특정 표준 서식 문자열에 해당 하는 사용자 지정 서식 문자열을 포함 하는 배열을 반환 합니다. 표준 형식 문자열 목록은 표준 날짜 및 시간 형식 문자열 을 참조 하세요.
서식 지정 작업에서 메서드에 의해 반환 된 배열에 사용자 지정 형식 문자열을 사용할 수 있습니다 GetAllDateTimePatterns . 그러나 이렇게 하면 및 메서드에서 해당 형식 지정 작업에서 반환 된 날짜 및 시간 값의 문자열 표현을 항상 성공적으로 구문 분석할 수 없습니다 Parse
TryParse
. 따라서 메서드에서 반환 된 사용자 지정 형식 문자열을 GetAllDateTimePatterns 사용 하 여 날짜 및 시간 값을 라운드트립 하는 것으로 간주할 수 없습니다. 다음 예제에서는 이 문제를 보여 줍니다. DateTimeFormatInfo러시아 (러시아) 문화권에 대 한 형식 정보를 포함 하는 개체를 검색 합니다. GetAllDateTimePatterns(Char)각 표준 서식 문자열에 대해 메서드를 호출한 다음 반환 된 배열의 각 사용자 지정 서식 문자열을 메서드에 전달 하 여 DateTime.ToString(String) 날짜 및 시간의 문자열 표현을 만듭니다. 그런 다음이 예제에서는 메서드를 호출 하 여이 값의 구문 분석을 시도 DateTime.TryParse(String, DateTime) 합니다. 예제의 출력에 표시 된 것 처럼 일부 사용자 지정 형식 문자열은 라운드트립 하는 날짜 및 시간 값을 생성 하지 않습니다.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo culture = CultureInfo.CreateSpecificCulture("ru-RU");
char[] formats = { 'd', 'D', 'f', 'F', 'g', 'G', 'm', 'o',
'r', 's', 't', 'T', 'u', 'U', 'y' };
DateTime date1 = new DateTime(2011, 02, 01, 7, 30, 45, 0);
DateTime date2;
int total = 0;
int noRoundTrip = 0;
foreach (var fmt in formats) {
total = 0;
noRoundTrip = 0;
foreach (var pattern in culture.DateTimeFormat.GetAllDateTimePatterns(fmt)) {
total++;
if (! DateTime.TryParse(date1.ToString(pattern), out date2)) {
noRoundTrip++;
Console.WriteLine("Unable to parse {0:" + pattern + "} (format '{1}')",
date1, pattern);
}
}
if (noRoundTrip > 0)
Console.WriteLine("{0}: Unable to round-trip {1} of {2} format strings.\n",
fmt, noRoundTrip, total);
else
Console.WriteLine("{0}: All custom format strings round trip.\n", fmt);
}
}
}
// The example displays the following output:
// d: All custom format strings round trip.
//
// Unable to parse 1 February 2011 г. (format 'd MMMM yyyy 'г.'')
// Unable to parse 01 February 2011 г. (format 'dd MMMM yyyy 'г.'')
// D: Unable to round-trip 2 of 2 format strings.
//
// Unable to parse 1 February 2011 г. 7:30 (format 'd MMMM yyyy 'г.' H:mm')
// Unable to parse 1 February 2011 г. 07:30 (format 'd MMMM yyyy 'г.' HH:mm')
// Unable to parse 01 February 2011 г. 7:30 (format 'dd MMMM yyyy 'г.' H:mm')
// Unable to parse 01 February 2011 г. 07:30 (format 'dd MMMM yyyy 'г.' HH:mm')
// f: Unable to round-trip 4 of 4 format strings.
//
// Unable to parse 1 February 2011 г. 7:30:45 (format 'd MMMM yyyy 'г.' H:mm:ss')
// Unable to parse 1 February 2011 г. 07:30:45 (format 'd MMMM yyyy 'г.' HH:mm:ss')
// Unable to parse 01 February 2011 г. 7:30:45 (format 'dd MMMM yyyy 'г.' H:mm:ss')
// Unable to parse 01 February 2011 г. 07:30:45 (format 'dd MMMM yyyy 'г.' HH:mm:ss')
// F: Unable to round-trip 4 of 4 format strings.
//
// g: All custom format strings round trip.
//
// G: All custom format strings round trip.
//
// m: All custom format strings round trip.
//
// o: All custom format strings round trip.
//
// r: All custom format strings round trip.
//
// s: All custom format strings round trip.
//
// t: All custom format strings round trip.
//
// T: All custom format strings round trip.
//
// u: All custom format strings round trip.
//
// Unable to parse 1 February 2011 г. 7:30:45 (format 'd MMMM yyyy 'г.' H:mm:ss')
// Unable to parse 1 February 2011 г. 07:30:45 (format 'd MMMM yyyy 'г.' HH:mm:ss')
// Unable to parse 01 February 2011 г. 7:30:45 (format 'dd MMMM yyyy 'г.' H:mm:ss')
// Unable to parse 01 February 2011 г. 07:30:45 (format 'dd MMMM yyyy 'г.' HH:mm:ss')
// U: Unable to round-trip 4 of 4 format strings.
//
// y: All custom format strings round trip.
Imports System.Globalization
Module Example
Public Sub Main()
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("ru-RU")
Dim formats() As String = { "d", "D", "f", "F", "g", "G", "m",
"o", "r", "s", "t", "T", "u", "U", "y" }
Dim date1 = New DateTime(2011, 02, 01, 7, 30, 45, 0)
Dim date2 As DateTime
Dim total, noRoundTrip As Integer
For Each fmt In formats
total = 0 : noRoundTrip = 0
For Each pattern In culture.DateTimeFormat.GetAllDateTimePatterns(CChar(fmt))
total += 1
If Not DateTime.TryParse(date1.ToString(pattern), date2)
noRoundTrip += 1
Console.WriteLine("Unable to parse {0:" + pattern + "} (format '{1}')",
date1, pattern)
End If
Next
If noRoundTrip > 0 Then
Console.WriteLine("{0}: Unable to round-trip {1} of {2} format strings.",
fmt, noRoundTrip, total)
Console.WriteLine()
Else
Console.WriteLine("{0}: All custom format strings round trip.", fmt)
Console.WriteLine()
End If
Next
End Sub
End Module
' The example displays the following output:
' d: All custom format strings round trip.
'
' Unable to parse 1 February 2011 г. (format 'd MMMM yyyy 'г.'')
' Unable to parse 01 February 2011 г. (format 'dd MMMM yyyy 'г.'')
' D: Unable to round-trip 2 of 2 format strings.
'
' Unable to parse 1 February 2011 г. 7:30 (format 'd MMMM yyyy 'г.' H:mm')
' Unable to parse 1 February 2011 г. 07:30 (format 'd MMMM yyyy 'г.' HH:mm')
' Unable to parse 01 February 2011 г. 7:30 (format 'dd MMMM yyyy 'г.' H:mm')
' Unable to parse 01 February 2011 г. 07:30 (format 'dd MMMM yyyy 'г.' HH:mm')
' f: Unable to round-trip 4 of 4 format strings.
'
' Unable to parse 1 February 2011 г. 7:30:45 (format 'd MMMM yyyy 'г.' H:mm:ss')
' Unable to parse 1 February 2011 г. 07:30:45 (format 'd MMMM yyyy 'г.' HH:mm:ss')
' Unable to parse 01 February 2011 г. 7:30:45 (format 'dd MMMM yyyy 'г.' H:mm:ss')
' Unable to parse 01 February 2011 г. 07:30:45 (format 'dd MMMM yyyy 'г.' HH:mm:ss')
' F: Unable to round-trip 4 of 4 format strings.
'
' g: All custom format strings round trip.
'
' G: All custom format strings round trip.
'
' m: All custom format strings round trip.
'
' o: All custom format strings round trip.
'
' r: All custom format strings round trip.
'
' s: All custom format strings round trip.
'
' t: All custom format strings round trip.
'
' T: All custom format strings round trip.
'
' u: All custom format strings round trip.
'
' Unable to parse 1 February 2011 г. 7:30:45 (format 'd MMMM yyyy 'г.' H:mm:ss')
' Unable to parse 1 February 2011 г. 07:30:45 (format 'd MMMM yyyy 'г.' HH:mm:ss')
' Unable to parse 01 February 2011 г. 7:30:45 (format 'dd MMMM yyyy 'г.' H:mm:ss')
' Unable to parse 01 February 2011 г. 07:30:45 (format 'dd MMMM yyyy 'г.' HH:mm:ss')
' U: Unable to round-trip 4 of 4 format strings.
'
' y: All custom format strings round trip.
미리 정의 된 여러 사용자 지정 형식으로 표현할 수 있는 날짜 및 시간의 문자열 표현을 구문 분석 하려면 다음 메서드 중 하나를 호출 합니다.
DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles)
DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles)
DateTime.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime)
DateTimeOffset.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset)
메서드에서 반환 된 배열의 사용자 지정 형식 문자열은 GetAllDateTimePatterns DateTimeFormatInfo 개체가 서식 지정 정보를 제공 하는 문화권의 현재 달력에 따라 달라 집니다. 일정이 변경 되는 경우이 메서드에서 반환 하는 배열도 변경 됩니다.