표준 날짜 및 시간 형식 문자열

업데이트: 2007년 11월

표준 날짜 및 시간 형식 문자열은 단일 표준 형식 지정자를 사용하여 형식 지정 작업으로 생성되는 날짜 및 시간 값의 텍스트 표현을 정의합니다. 공백을 포함하여 문자가 두 개 이상 포함된 날짜 및 시간 형식 문자열은 사용자 지정 날짜 및 시간 형식 문자열로 해석됩니다.

참고:

표준 날짜 및 시간 형식 문자열은 DateTimeDateTimeOffset 값 모두에 사용할 수 있습니다.

표준 형식 문자열의 작동 방법

표준 형식 문자열은 사용자 지정 형식 문자열의 별칭입니다. 별칭을 사용하여 사용자 지정 형식 문자열을 참조할 경우 별칭은 변하지 않지만 사용자 지정 형식 문자열 자체는 변경될 수 있다는 장점이 있습니다. 이는 날짜 및 시간 값에 대한 문자열 표현이 일반적으로 문화권마다 다르다는 점을 감안했을 때 매우 중요한 기능입니다. 예를 들어, d 표준 형식 문자열은 날짜 및 시간 값을 간단한 날짜 패턴을 사용하여 표시함을 나타냅니다. 고정 문화권의 경우 이 패턴이 "MM/dd/yyyy"이고 fr-FR 문화권의 경우 "dd/MM/yyyy"이며 ja-JP 문화권의 경우 "yyyy/MM/dd"입니다.

표준 형식 문자열이 특정 문화권의 사용자 지정 형식 문자열에 매핑되는 경우 응용 프로그램에서는 다음 방법 중 하나로 사용할 사용자 지정 형식 문자열이 포함된 특정 문화권을 정의할 수 있습니다.

  • 기본 또는 현재 문화권을 사용할 수 있습니다. 다음 예제에서는 현재 문화권의 간단한 날짜 형식을 사용하여 날짜를 표시합니다. 이 예제의 경우 현재 문화권이 en-US입니다.

    ' Display using current (en-us) culture's short date format
    Dim thisDate As Date = #03/15/2008#
    Console.WriteLine(thisDate.ToString("d"))     ' Displays 3/15/2008
    
    // Display using current (en-us) culture's short date format
    DateTime thisDate = new DateTime(2008, 3, 15);
    Console.WriteLine(thisDate.ToString("d"));           // Displays 3/15/2008
    
  • 사용할 형식 지정과 관련된 문화권을 나타내는 CultureInfo 개체를 IFormatProvider 매개 변수와 함께 메서드에 전달할 수 있습니다. 다음 예제에서는 pt-BR 문화권의 간단한 날짜 형식을 사용하여 날짜를 표시합니다.

    ' Display using pt-BR culture's short date format
    Dim thisDate As Date = #03/15/2008#
    Dim culture As New CultureInfo("pt-BR")      
    Console.WriteLine(thisDate.ToString("d", culture))   ' Displays 15/3/2008
    
    // Display using pt-BR culture's short date format
    DateTime thisDate = new DateTime(2008, 3, 15);
    CultureInfo culture = new CultureInfo("pt-BR");      
    Console.WriteLine(thisDate.ToString("d", culture));  // Displays 15/3/2008
    
  • 형식 지정 정보를 제공하는 DateTimeFormatInfo 개체를 IFormatProvider 매개 변수와 함께 메서드에 전달할 수 있습니다. 다음 예제에서는 hr-HR 문화권의 DateTimeFormatInfo 개체가 제공하는 간단한 날짜 형식을 사용하여 날짜를 표시합니다.

    ' Display using date format information from hr-HR culture
    Dim thisDate As Date = #03/15/2008#
    Dim fmt As DateTimeFormatInfo = (New CultureInfo("hr-HR")).DateTimeFormat
    Console.WriteLine(thisDate.ToString("d", fmt))   ' Displays 15.3.2008
    
    // Display using date format information from hr-HR culture
    DateTime thisDate = new DateTime(2008, 3, 15);
    DateTimeFormatInfo fmt = (new CultureInfo("hr-HR")).DateTimeFormat;
    Console.WriteLine(thisDate.ToString("d", fmt));      // Displays 15.3.2008
    

표준 형식 문자열을 보다 긴 고정 사용자 지정 형식 문자열에 대한 약식 표현으로 사용하는 경우도 있습니다. 표준 형식 문자열은 O(또는 o), R(또는 r), s, 및 u라는 네 범주로 나뉩니다. 이 문자열은 고정 문화권에 정의된 사용자 지정 형식 문자열에 대응되며, 문화권마다 동일하게 인식되는 날짜 및 시간 값에 대한 문자열 표현을 생성합니다. 다음 표에서는 네 가지 표준 날짜 및 시간 형식 지정자에 대해 설명합니다.

표준 형식 문자열

DateTimeFormatInfo.InvariantInfo 속성으로 정의된 문자열

사용자 지정 형식 문자열

O 또는 o

없음

yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzz

R 또는 r

RFC1123Pattern

ddd, dd MMM yyyy HH':'mm':'ss 'GMT'

s

SortableDateTimePattern

yyyy'-'MM'-'dd'T'HH':'mm':'ss

u

UniversalSortableDateTimePattern

yyyy'-'MM'-'dd HH':'mm':'ss'Z'

표준 날짜 및 시간 형식 지정자

다음 표에서는 표준 날짜 및 시간 형식 지정자에 대해 설명합니다. 다른 설명이 없는 한 특정 표준 날짜 및 시간 형식 지정자는 DateTime 값에 사용할 때와 DateTimeOffset 값에 사용할 때 동일한 문자열을 생성합니다.

형식 지정자

이름

설명

d

간단한 날짜 패턴

현재 ShortDatePattern 속성으로 정의된 사용자 지정 날짜 및 시간 형식 문자열을 나타냅니다. 예를 들어, 고정 문화권에 대해 ShortDatePattern 속성이 반환하는 사용자 지정 형식 문자열은 "MM/dd/yyyy"입니다.

다음 예제에서는 d 형식 지정자를 사용하여 날짜 및 시간 값을 표시합니다.

Dim date1 As Date = #4/10/2008#
Console.WriteLine(date1.ToString("d", DateTimeFormatInfo.InvariantInfo))
' Displays 04/10/2008
Console.WriteLine(date1.ToString("d", _
CultureInfo.CreateSpecificCulture("en-US")))
' Displays 4/10/2008
Console.WriteLine(date1.ToString("d", _
CultureInfo.CreateSpecificCulture("en-NZ")))
' Displays 10/04/2008
Console.WriteLine(date1.ToString("d", _
CultureInfo.CreateSpecificCulture("de-DE")))
' Displays 10.04.2008
DateTime date1 = new DateTime(2008,4, 10);
Console.WriteLine(date1.ToString("d", DateTimeFormatInfo.InvariantInfo));
// Displays 04/10/2008
Console.WriteLine(date1.ToString("d",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays 4/10/2008
Console.WriteLine(date1.ToString("d",
CultureInfo.CreateSpecificCulture("en-NZ")));
// Displays 10/04/2008
Console.WriteLine(date1.ToString("d",
CultureInfo.CreateSpecificCulture("de-DE")));
// Displays 10.04.2008

D

자세한 날짜 패턴

현재 LongDatePattern 속성으로 정의된 사용자 지정 날짜 및 시간 형식 문자열을 나타냅니다. 예를 들어, 고정 문화권에 대한 사용자 지정 형식 문자열은 "dddd, dd MMMM yyyy"입니다.

다음 예제에서는 D 형식 지정자를 사용하여 날짜 및 시간 값을 표시합니다.

Dim date1 As Date = #4/10/2008#
Console.WriteLine(date1.ToString("D", _
CultureInfo.CreateSpecificCulture("en-US")))
' Displays Thursday, April 10, 2008
Console.WriteLine(date1.ToString("D", _
CultureInfo.CreateSpecificCulture("pt-BR")))
' Displays quinta-feira, 10 de abril de 2008
Console.WriteLine(date1.ToString("D", _
CultureInfo.CreateSpecificCulture("es-MX")))
' Displays jueves, 10 de abril de 2008
DateTime date1 = new DateTime(2008, 4, 10);
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("pt-BR")));
// Displays quinta-feira, 10 de abril de 2008
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("es-MX")));
// Displays jueves, 10 de abril de 2008

f

전체 날짜/시간 패턴(간단한 시간)

자세한 날짜(D) 패턴과 간단한 시간(t) 패턴을 공백으로 구분하여 조합한 형식을 나타냅니다.

다음 예제에서는 f 형식 지정자를 사용하여 날짜 및 시간 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("f", _
CultureInfo.CreateSpecificCulture("en-US")))
' Displays Thursday, April 10, 2008 6:30 AM
Console.WriteLine(date1.ToString("f", _
CultureInfo.CreateSpecificCulture("fr-FR")))
' Displays jeudi 10 avril 2008 06:30
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("f",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008 6:30 AM
Console.WriteLine(date1.ToString("f",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays jeudi 10 avril 2008 06:30

F

전체 날짜/시간 패턴(자세한 시간)

현재 FullDateTimePattern 속성으로 정의된 사용자 지정 날짜 및 시간 형식 문자열을 나타냅니다. 예를 들어, 고정 문화권에 대한 사용자 지정 형식 문자열은 "dddd, dd MMMM yyyy HH:mm:ss"입니다.

다음 예제에서는 F 형식 지정자를 사용하여 날짜 및 시간 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("F", _
CultureInfo.CreateSpecificCulture("en-US")))
' Displays Thursday, April 10, 2008 6:30:00 AM
Console.WriteLine(date1.ToString("F", _
CultureInfo.CreateSpecificCulture("fr-FR")))
' Displays jeudi 10 avril 2008 06:30:00
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("F",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008 6:30:00 AM
Console.WriteLine(date1.ToString("F",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays jeudi 10 avril 2008 06:30:00

g

일반 날짜/시간 패턴(간단한 시간)

간단한 날짜(d) 패턴과 간단한 시간(t) 패턴을 공백으로 구분하여 조합한 형식을 나타냅니다. 다음 예제에서는 g 형식 지정자를 사용하여 날짜 및 시간 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("g", _
DateTimeFormatInfo.InvariantInfo))
' Displays 04/10/2008 06:30
Console.WriteLine(date1.ToString("g", _
CultureInfo.CreateSpecificCulture("en-us")))
' Displays 4/10/2008 6:30 AM
Console.WriteLine(date1.ToString("g", _
CultureInfo.CreateSpecificCulture("fr-BE")))
' Displays10/04/2008 6:30
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("g",
DateTimeFormatInfo.InvariantInfo));
// Displays 04/10/2008 06:30
Console.WriteLine(date1.ToString("g",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 4/10/2008 6:30 AM
Console.WriteLine(date1.ToString("g",
CultureInfo.CreateSpecificCulture("fr-BE")));
// Displays10/04/2008 6:30

G

일반 날짜/시간 패턴(자세한 시간)

간단한 날짜(d) 패턴과 자세한 시간(t) 패턴을 공백으로 구분하여 조합한 형식을 나타냅니다. 다음 예제에서는 G 형식 지정자를 사용하여 날짜 및 시간 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("G", _
DateTimeFormatInfo.InvariantInfo))
' Displays 04/10/2008 06:30:00
Console.WriteLine(date1.ToString("G", _
CultureInfo.CreateSpecificCulture("en-us")))
' Displays 4/10/2008 6:30:00 AM
Console.WriteLine(date1.ToString("G", _
CultureInfo.CreateSpecificCulture("nl-BE")))
' Displays 10/04/2008 6:30:00
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("G",
DateTimeFormatInfo.InvariantInfo));
// Displays 04/10/2008 06:30:00
Console.WriteLine(date1.ToString("G",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 4/10/2008 6:30:00 AM
Console.WriteLine(date1.ToString("G",
CultureInfo.CreateSpecificCulture("nl-BE")));
// Displays 10/04/2008 6:30:00

M, m

월 일 패턴

현재 MonthDayPattern 속성으로 정의된 사용자 지정 날짜 및 시간 형식 문자열을 나타냅니다. 예를 들어, 고정 문화권에 대한 사용자 지정 형식 문자열은 "MMMM dd"입니다. 다음 예제에서는 G 형식 지정자를 사용하여 날짜 및 시간 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("m", _
CultureInfo.CreateSpecificCulture("en-us")))
' Displays April 10
Console.WriteLine(date1.ToString("m", _
CultureInfo.CreateSpecificCulture("ms-MY")))
' Displays 10 April
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("m",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays April 10
Console.WriteLine(date1.ToString("m",
CultureInfo.CreateSpecificCulture("ms-MY")));
// Displays 10 April

O, o

라운드트립 날짜/시간 패턴

표준 시간대 정보를 유지하는 패턴을 사용하여 사용자 지정 날짜 및 시간 형식 문자열을 나타냅니다. DateTime 값의 경우 이 형식 지정자는 Kind 속성과 함께 날짜 및 시간 값을 텍스트로 유지합니다. 그런 다음 형식이 지정된 문자열을 Parse 또는 ParseExact와 올바른 Kind 속성 값을 사용하여 다시 구문 분석할 수 있습니다.

DateTime 값에 대한 사용자 지정 형식 문자열은 "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK"이고 DateTimeOffset 값에 대한 사용자 지정 형식 문자열은 "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz"입니다. 이 문자열에서 하이픈, 콜론, 문자 "T" 등의 개별 문자를 구분하는 아포스트로피 쌍은 변경할 수 없는 리터럴입니다. 아포스트로피 자체는 출력 문자열에 표시되지 않습니다.

이 지정자의 패턴은 표준 (ISO 8601)으로 정의되어 있습니다. 따라서 이 패턴은 사용된 문화권이나 제공된 형식 공급자에 관계없이 항상 같습니다. Parse 또는 ParseExact 메서드에 전달된 문자열은 이 사용자 지정 형식 패턴을 따라야 하며, 그렇지 않으면 FormatException이 throw됩니다.

이 표준 형식 지정자를 사용할 경우 형식 지정 또는 구문 분석 작업에서 항상 고정 문화권이 사용됩니다.

다음 예제에서는 o 형식 지정자를 사용하여 미국 태평양 표준 시간대에 있는 시스템의 DateTimeDateTimeOffset 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Dim dateOffset As New DateTimeOffset(date1, TimeZoneInfo.Local.GetUtcOFfset(date1))
Console.WriteLine(date1.ToString("o"))
' Displays 2008-04-10T06:30:00.0000000
Console.WriteLine(dateOffset.ToString("o"))
' Displays 2008-04-10T06:30:00.0000000-07:00
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
DateTimeOffset dateOffset = new DateTimeOffset(date1,
TimeZoneInfo.Local.GetUtcOffset(date1));
Console.WriteLine(date1.ToString("o"));
// Displays 2008-04-10T06:30:00.0000000
Console.WriteLine(dateOffset.ToString("o"));
// Displays 2008-04-10T06:30:00.0000000-07:00

R, r

RFC1123 패턴

DateTimeFormatInfo.RFC1123Pattern 속성으로 정의된 사용자 지정 날짜 및 시간 형식 문자열을 나타냅니다. 이 패턴은 표준으로 정의되어 있으며 해당 속성은 읽기 전용입니다. 따라서 이 패턴은 사용된 문화권이나 제공된 형식 공급자에 관계없이 항상 같습니다. 사용자 지정 형식 문자열은 "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"입니다.

이 표준 형식 지정자를 사용할 경우 형식 지정 또는 구문 분석 작업에서 항상 고정 문화권이 사용됩니다.

형식을 지정해도 해당 형식이 지정되는 DateTime 또는 DateTimeOffset 개체의 값은 수정되지 않습니다. 따라서 응용 프로그램에서는 이 형식 패턴을 사용하기 전에 값을 UTC(협정 세계시)로 변환해야 합니다.

다음 예제에서는 r 형식 지정자를 사용하여 미국 태평양 표준 시간대에 있는 시스템의 DateTimeDateTimeOffset 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Dim dateOffset As New DateTimeOffset(date1, TimeZoneInfo.Local.GetUtcOFfset(date1))
Console.WriteLine(date1.ToUniversalTime.ToString("r"))
' Displays Thu, 10 Apr 2008 13:30:00 GMT
Console.WriteLine(dateOffset.ToUniversalTime.ToString("r"))
' Displays Thu, 10 Apr 2008 13:30:00 GMT
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
DateTimeOffset dateOffset = new DateTimeOffset(date1,
TimeZoneInfo.Local.GetUtcOffset(date1));
Console.WriteLine(date1.ToUniversalTime().ToString("r"));
// Displays Thu, 10 Apr 2008 13:30:00 GMT
Console.WriteLine(dateOffset.ToUniversalTime().ToString("r"));
// Displays Thu, 10 Apr 2008 13:30:00 GMT

s

정렬 가능한 날짜/시간 패턴; ISO 8601에 부합

DateTimeFormatInfo.SortableDateTimePattern 속성으로 정의된 사용자 지정 날짜 및 시간 형식 문자열을 나타냅니다. 이 패턴은 표준으로 정의되어 있으며 해당 속성은 읽기 전용입니다. 따라서 이 패턴은 사용된 문화권이나 제공된 형식 공급자에 관계없이 항상 같습니다. 사용자 지정 형식 문자열은 "yyyy'-'MM'-'dd'T'HH':'mm':'ss"입니다.

이 표준 형식 지정자를 사용할 경우 형식 지정 또는 구문 분석 작업에서 항상 고정 문화권이 사용됩니다.

다음 예제에서는 s 형식 지정자를 사용하여 미국 태평양 표준 시간대에 있는 시스템의 DateTimeDateTimeOffset 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("s"))
' Displays 2008-04-10T06:30:00
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("s"));
// Displays 2008-04-10T06:30:00

t

간단한 시간 패턴

현재 ShortTimePattern 속성으로 정의된 사용자 지정 날짜 및 시간 형식 문자열을 나타냅니다. 예를 들어, 고정 문화권에 대한 사용자 지정 형식 문자열은 "HH:mm"입니다.

다음 예제에서는 t 형식 지정자를 사용하여 날짜 및 시간 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("t", _
CultureInfo.CreateSpecificCulture("en-us")))
' Displays 6:30 AM
Console.WriteLine(date1.ToString("t", _
CultureInfo.CreateSpecificCulture("es-ES")))
' Displays 6:30
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("t",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 6:30 AM
Console.WriteLine(date1.ToString("t",
CultureInfo.CreateSpecificCulture("es-ES")));
// Displays 6:30

T

자세한 시간 패턴

현재 LongTimePattern 속성으로 정의된 사용자 지정 날짜 및 시간 형식 문자열을 나타냅니다. 예를 들어, 고정 문화권에 대한 사용자 지정 형식 문자열은 "HH:mm:ss"입니다.

다음 예제에서는 T 형식 지정자를 사용하여 날짜 및 시간 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("T", _
CultureInfo.CreateSpecificCulture("en-us")))
' Displays 6:30:00 AM
Console.WriteLine(date1.ToString("T", _
CultureInfo.CreateSpecificCulture("es-ES")))
' Displays 6:30:00
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("T",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 6:30:00 AM
Console.WriteLine(date1.ToString("T",
CultureInfo.CreateSpecificCulture("es-ES")));
// Displays 6:30:00

u

정렬 가능한 유니버설 날짜/시간 패턴

DateTimeFormatInfo.UniversalSortableDateTimePattern 속성으로 정의된 사용자 지정 날짜 및 시간 형식 문자열을 나타냅니다. 이 패턴은 표준으로 정의되어 있으며 해당 속성은 읽기 전용입니다. 따라서 이 패턴은 사용된 문화권이나 제공된 형식 공급자에 관계없이 항상 같습니다. 사용자 지정 형식 문자열은 "yyyy'-'MM'-'dd HH':'mm':'ss'Z"입니다.

이 표준 형식 지정자를 사용할 경우 형식 지정 또는 구문 분석 작업에서 항상 고정 문화권이 사용됩니다.

날짜 및 시간 개체의 형식이 지정될 때 표준 시간대는 변환되지 않습니다. 따라서 응용 프로그램에서는 이 형식 지정자를 사용하기 전에 날짜 및 시간을 UTC(협정 세계시)로 변환해야 합니다.

다음 예제에서는 u 형식 지정자를 사용하여 날짜 및 시간 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToUniversalTime.ToString("u"))
' Displays 2008-04-10 13:30:00Z
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToUniversalTime().ToString("u"));
// Displays 2008-04-10 13:30:00Z

U

유니버설 전체 날짜/시간 패턴

현재 FullDateTimePattern 속성으로 정의된 사용자 지정 날짜 및 시간 형식 문자열을 나타냅니다.

이 패턴은 F 패턴과 동일합니다. 그러나 형식 지정 작업이 DateTime 값에 해당하는 UTC에 대해 수행된다는 점이 다릅니다.

U 형식 지정자는 DateTimeOffset 형식에는 지원되지 않으므로 U 형식 지정자를 사용하여 DateTimeOffset 값의 형식을 지정하려고 하면 FormatException이 throw됩니다.

다음 예제에서는 T 형식 지정자를 사용하여 날짜 및 시간 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("U", CultureInfo.CreateSpecificCulture("en-US")))
' Displays Thursday, April 10, 2008 1:30:00 PM
Console.WriteLine(date1.ToString("U", CultureInfo.CreateSpecificCulture("sv-FI")))
' Displays den 10 april 2008 13:30:00
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("U",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008 1:30:00 PM
Console.WriteLine(date1.ToString("U",
CultureInfo.CreateSpecificCulture("sv-FI")));
// Displays den 10 april 2008 13:30:00

Y, y

년 월 패턴

현재 YearMonthPattern 속성으로 정의된 사용자 지정 날짜 및 시간 형식 문자열을 나타냅니다. 예를 들어, 고정 문화권에 대한 사용자 지정 형식 문자열은 "yyyy MMMM"입니다.

다음 예제에서는 y 형식 지정자를 사용하여 날짜 및 시간 값을 표시합니다.

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("Y", CultureInfo.CreateSpecificCulture("en-US")))
' Displays April, 2008
Console.WriteLine(date1.ToString("y", CultureInfo.CreateSpecificCulture("af-ZA")))
' Displays April 2008
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("Y",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays April, 2008
Console.WriteLine(date1.ToString("y",
CultureInfo.CreateSpecificCulture("af-ZA")));
// Displays April 2008

기타 모든 단일 문자

알 수 없는 지정자

런타임 FormatException을 throw합니다.

제어판 설정

제어판에 있는 국가 및 언어 옵션의 설정은 형식 지정 작업으로 생성되는 결과 문자열에 영향을 줍니다. 이러한 설정은 형식을 제어하는 데 사용되는 값을 제공하는 현재 스레드 문화권과 연결된 DateTimeFormatInfo 개체를 초기화하는 데 사용됩니다. 다른 설정을 사용하는 컴퓨터는 다른 결과 문자열을 생성합니다.

또한 현재 시스템 문화권과 같은 문화권을 나타내는 새 CultureInfo 개체를 인스턴스화하는 데 CultureInfo.CultureInfo(String) 생성자가 사용된 경우 제어판의 국가 및 언어 옵션 항목을 통해 설정된 사용자 지정 내용이 새 CultureInfo 개체에도 적용됩니다. CreateSpecificCulture 메서드를 사용하면 시스템의 사용자 지정된 내용이 반영되지 않은 CultureInfo 개체를 만들 수 있습니다.

DateTimeFormatInfo 속성

형식 지정 작업은 현재 DateTimeFormatInfo 개체의 속성에 따라 영향을 받으며 이 개체는 현재 스레드 문화권에 의해 암시적으로 제공되거나 형식 지정 작업을 호출하는 메서드의 IFormatProvider 매개 변수를 통해 명시적으로 제공됩니다. IFormatProvider 매개 변수에 대해 응용 프로그램에서는 문화권을 나타내는 CultureInfo 개체를 지정하거나 특정 문화권의 날짜 및 시간 형식 지정 규칙을 나타내는 DateTimeFormatInfo 개체를 지정해야 합니다. 대부분의 표준 날짜 및 시간 형식 지정자는 현재 DateTimeFormatInfo 개체의 속성으로 정의된 형식 지정 패턴의 별칭입니다. 따라서 응용 프로그램에서는 해당 DateTimeFormatInfo 속성의 날짜 및 시간 형식 패턴을 변경하여 일부 표준 날짜 및 시간 형식 지정자로 생성되는 결과를 변경할 수 있습니다.

표준 형식 문자열 사용

다음 코드에서는 표준 형식 문자열을 DateTime 값에 사용하는 방법을 보여 줍니다.

Dim dt As DateTime = DateTime.Now
Dim dfi As DateTimeFormatInfo = New DateTimeFormatInfo()
Dim ci As CultureInfo = New CultureInfo("de-DE")

' Create a new custom DateTime pattern for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd"

' Use the DateTimeFormat from the culture associated 
' with the current thread.

Console.WriteLine( dt.ToString("d") )  
Console.WriteLine( dt.ToString("m") )

' Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) )

' Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) )

' Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-BE")
Console.WriteLine( dt.ToString("d") )

' Use a CultureInfo with a format specifier to parse a string.
Dim culter As New CultureInfo("en-US")
Dim myDateTime As DateTime 
myDateTime = DateTime.ParseExact("Tuesday, April 10, 2001", "D", culter)
Console.WriteLine(myDateTime.ToString("D"))
DateTime dt = DateTime.Now;
DateTimeFormatInfo dfi = new DateTimeFormatInfo();
CultureInfo ci = new CultureInfo("de-DE");

// Create a new custom DateTime pattern for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd";

// Use the DateTimeFormat from the culture associated 
// with the current thread.
Console.WriteLine( dt.ToString("d") );  
Console.WriteLine( dt.ToString("m") );

// Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) );

// Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) );

// Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-BE");
Console.WriteLine( dt.ToString("d") );

// Use a CultureInfo with a format specifier to parse a string.
IFormatProvider culter = new CultureInfo("en-US");
DateTime myDateTime = DateTime.ParseExact("Tuesday, April 10, 2001", "D", culter);
Console.WriteLine(myDateTime.ToString("D"));

예제

다음 예제에서는 스레드 현재 문화권, 지정된 문화권, 모든 표준 날짜 및 시간 형식 지정자를 사용하여 DateTime 개체의 형식을 지정합니다.

' This code example demonstrates the ToString(String) and 
' ToString(String, IFormatProvider) methods for the DateTime 
' type in conjunction with the standard date and time 
' format specifiers.

Imports System
Imports System.Globalization
Imports System.Threading

Class Sample
    Public Shared Sub Main() 
        Dim msgShortDate As String = "(d) Short date: . . . . . . . "
        Dim msgLongDate As String  = "(D) Long date:. . . . . . . . "
        Dim msgShortTime As String = "(t) Short time: . . . . . . . "
        Dim msgLongTime As String  = "(T) Long time:. . . . . . . . "
        Dim msgFullDateShortTime As String = _
                                     "(f) Full date/short time: . . "
        Dim msgFullDateLongTime As String = _
                                     "(F) Full date/long time:. . . "
        Dim msgGeneralDateShortTime As String = _
                                     "(g) General date/short time:. "
        Dim msgGeneralDateLongTime As String = _
                                     "(G) General date/long time (default):" & vbCrLf & _
                                     "    . . . . . . . . . . . . . "
        Dim msgMonth As String     = "(M) Month:. . . . . . . . . . "
        Dim msgRFC1123 As String   = "(R) RFC1123:. . . . . . . . . "
        Dim msgSortable As String  = "(s) Sortable: . . . . . . . . "
        Dim msgUniSortInvariant As String = _
                                     "(u) Universal sortable (invariant):" & vbCrLf & _
                                     "    . . . . . . . . . . . . . "
        Dim msgUniFull As String   = "(U) Universal full date/time: "
        Dim msgYear As String      = "(Y) Year: . . . . . . . . . . "

        Dim msgRoundtripLocal As String         = "(o) Roundtrip (local):. . . . "
        Dim msgRoundtripUTC As String           = "(o) Roundtrip (UTC):. . . . . "
        Dim msgRoundtripUnspecified As String   = "(o) Roundtrip (Unspecified):. "


        Dim msg1 As String = "Use ToString(String) and the current thread culture." & vbCrLf
        Dim msg2 As String = "Use ToString(String, IFormatProvider) and a specified culture." & vbCrLf
        Dim msgCulture As String   = "Culture:"
        Dim msgThisDate As String  = "This date and time: {0}" & vbCrLf

        Dim thisDate As DateTime = DateTime.Now
        Dim  utcDate As DateTime = thisDate.ToUniversalTime()
        Dim unspecifiedDate As DateTime = new DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind.Unspecified)
        Dim ci As CultureInfo

        ' Format the current date and time in various ways.
        Console.Clear()
        Console.WriteLine("Standard DateTime Format Specifiers:" & vbCrLf)
        Console.WriteLine(msgThisDate, thisDate)
        Console.WriteLine(msg1)

        ' Display the thread current culture, which is used to format the values.
        ci = Thread.CurrentThread.CurrentCulture
        Console.WriteLine("{0,-30}{1}" & vbCrLf, msgCulture, ci.DisplayName)

        Console.WriteLine(msgShortDate            &        thisDate.ToString("d"))
        Console.WriteLine(msgLongDate             &        thisDate.ToString("D"))
        Console.WriteLine(msgShortTime            &        thisDate.ToString("t"))
        Console.WriteLine(msgLongTime             &        thisDate.ToString("T"))
        Console.WriteLine(msgFullDateShortTime    &        thisDate.ToString("f"))
        Console.WriteLine(msgFullDateLongTime     &        thisDate.ToString("F"))
        Console.WriteLine(msgGeneralDateShortTime &        thisDate.ToString("g"))
        Console.WriteLine(msgGeneralDateLongTime  &        thisDate.ToString("G"))
        Console.WriteLine(msgMonth                &        thisDate.ToString("M"))
        Console.WriteLine(msgRFC1123              &         utcDate.ToString("R"))
        Console.WriteLine(msgSortable             &        thisDate.ToString("s"))
        Console.WriteLine(msgUniSortInvariant     &         utcDate.ToString("u"))
        Console.WriteLine(msgUniFull              &        thisDate.ToString("U"))
        Console.WriteLine(msgYear                 &        thisDate.ToString("Y"))
        Console.WriteLine(msgRoundtripLocal       &        thisDate.ToString("o"))
        Console.WriteLine(msgRoundtripUTC         &         utcDate.ToString("o"))
        Console.WriteLine(msgRoundtripUnspecified & unspecifiedDate.ToString("o"))

        Console.WriteLine()

        ' Display the same values using a CultureInfo object. The CultureInfo class 
        ' implements IFormatProvider.
        Console.WriteLine(msg2)

        ' Display the culture used to format the values. 
        ci = New CultureInfo("de-DE")
        Console.WriteLine("{0,-30}{1}" & vbCrLf, msgCulture, ci.DisplayName)

        Console.WriteLine(msgShortDate            &        thisDate.ToString("d", ci))
        Console.WriteLine(msgLongDate             &        thisDate.ToString("D", ci))
        Console.WriteLine(msgShortTime            &        thisDate.ToString("t", ci))
        Console.WriteLine(msgLongTime             &        thisDate.ToString("T", ci))
        Console.WriteLine(msgFullDateShortTime    &        thisDate.ToString("f", ci))
        Console.WriteLine(msgFullDateLongTime     &        thisDate.ToString("F", ci))
        Console.WriteLine(msgGeneralDateShortTime &        thisDate.ToString("g", ci))
        Console.WriteLine(msgGeneralDateLongTime  &        thisDate.ToString("G", ci))
        Console.WriteLine(msgMonth                &        thisDate.ToString("M", ci))
        Console.WriteLine(msgRFC1123              &         utcDate.ToString("R", ci))
        Console.WriteLine(msgSortable             &        thisDate.ToString("s", ci))
        Console.WriteLine(msgUniSortInvariant     &         utcDate.ToString("u", ci))
        Console.WriteLine(msgUniFull              &        thisDate.ToString("U", ci))
        Console.WriteLine(msgYear                 &        thisDate.ToString("Y", ci))
        Console.WriteLine(msgRoundtripLocal       &        thisDate.ToString("o"), ci)
        Console.WriteLine(msgRoundtripUTC         &         utcDate.ToString("o"), ci)
        Console.WriteLine(msgRoundtripUnspecified & unspecifiedDate.ToString("o"), ci)

        Console.WriteLine()

    End Sub 'Main
End Class 'Sample
'
'This code example produces the following results:
'
'Standard DateTime Format Specifiers:
'
'This date and time: 4/17/2006 2:29:09 PM
'
'Use ToString(String) and the current thread culture.
'
'Culture:                      English (United States)
'
'(d) Short date: . . . . . . . 4/17/2006
'(D) Long date:. . . . . . . . Monday, April 17, 2006
'(t) Short time: . . . . . . . 2:29 PM
'(T) Long time:. . . . . . . . 2:29:09 PM
'(f) Full date/short time: . . Monday, April 17, 2006 2:29 PM
'(F) Full date/long time:. . . Monday, April 17, 2006 2:29:09 PM
'(g) General date/short time:. 4/17/2006 2:29 PM
'(G) General date/long time (default):
'    . . . . . . . . . . . . . 4/17/2006 2:29:09 PM
'(M) Month:. . . . . . . . . . April 17
'(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:29:09 GMT
'(s) Sortable: . . . . . . . . 2006-04-17T14:29:09
'(u) Universal sortable (invariant):
'    . . . . . . . . . . . . . 2006-04-17 21:29:09Z
'(U) Universal full date/time: Monday, April 17, 2006 9:29:09 PM
'(Y) Year: . . . . . . . . . . April, 2006
'(o) Roundtrip (local):. . . . 2006-04-17T14:29:09.3011250-07:00
'(o) Roundtrip (UTC):. . . . . 2006-04-17T21:29:09.3011250Z
'(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000
'
'Use ToString(String, IFormatProvider) and a specified culture.
'
'Culture:                      German (Germany)
'
'(d) Short date: . . . . . . . 17.04.2006
'(D) Long date:. . . . . . . . Montag, 17. April 2006
'(t) Short time: . . . . . . . 14:29
'(T) Long time:. . . . . . . . 14:29:09
'(f) Full date/short time: . . Montag, 17. April 2006 14:29
'(F) Full date/long time:. . . Montag, 17. April 2006 14:29:09
'(g) General date/short time:. 17.04.2006 14:29
'(G) General date/long time (default):
'    . . . . . . . . . . . . . 17.04.2006 14:29:09
'(M) Month:. . . . . . . . . . 17 April
'(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:29:09 GMT
'(s) Sortable: . . . . . . . . 2006-04-17T14:29:09
'(u) Universal sortable (invariant):
'    . . . . . . . . . . . . . 2006-04-17 21:29:09Z
'(U) Universal full date/time: Montag, 17. April 2006 21:29:09
'(Y) Year: . . . . . . . . . . April 2006
'(o) Roundtrip (local):. . . . 2006-04-17T14:29:09.3011250-07:00
'(o) Roundtrip (UTC):. . . . . 2006-04-17T21:29:09.3011250Z
'(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000
// This code example demonstrates the ToString(String) and 
// ToString(String, IFormatProvider) methods for the DateTime 
// type in conjunction with the standard date and time 
// format specifiers.

using System;
using System.Globalization;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
    string msgShortDate = "(d) Short date: . . . . . . . ";
    string msgLongDate  = "(D) Long date:. . . . . . . . ";
    string msgShortTime = "(t) Short time: . . . . . . . ";
    string msgLongTime  = "(T) Long time:. . . . . . . . ";
    string msgFullDateShortTime = 
                          "(f) Full date/short time: . . ";
    string msgFullDateLongTime =
                          "(F) Full date/long time:. . . ";
    string msgGeneralDateShortTime = 
                          "(g) General date/short time:. ";
    string msgGeneralDateLongTime = 
                          "(G) General date/long time (default):\n" +
                          "    . . . . . . . . . . . . . ";
    string msgMonth   =   "(M) Month:. . . . . . . . . . ";
    string msgRFC1123 =   "(R) RFC1123:. . . . . . . . . ";
    string msgSortable =  "(s) Sortable: . . . . . . . . ";
    string msgUniSortInvariant = 
                          "(u) Universal sortable (invariant):\n" + 
                          "    . . . . . . . . . . . . . ";
    string msgUniFull =   "(U) Universal full date/time: ";
    string msgYear =      "(Y) Year: . . . . . . . . . . ";
    string msgRoundtripLocal        = "(o) Roundtrip (local):. . . . ";
    string msgRoundtripUTC          = "(o) Roundtrip (UTC):. . . . . ";
    string msgRoundtripUnspecified  = "(o) Roundtrip (Unspecified):. ";


    string msg1 = "Use ToString(String) and the current thread culture.\n";
    string msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n";
    string msgCulture  = "Culture:";
    string msgThisDate = "This date and time: {0}\n";

    DateTime thisDate  = DateTime.Now;
    DateTime  utcDate  = thisDate.ToUniversalTime();
    DateTime unspecifiedDate = new DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind.Unspecified);
    CultureInfo ci;

// Format the current date and time in various ways.
    Console.Clear();
    Console.WriteLine("Standard DateTime Format Specifiers:\n");
    Console.WriteLine(msgThisDate, thisDate);
    Console.WriteLine(msg1);

// Display the thread current culture, which is used to format the values.
    ci = Thread.CurrentThread.CurrentCulture;
    Console.WriteLine("{0,-30}{1}\n", msgCulture, ci.DisplayName);

    Console.WriteLine(msgShortDate            +         thisDate.ToString("d"));
    Console.WriteLine(msgLongDate             +         thisDate.ToString("D"));
    Console.WriteLine(msgShortTime            +         thisDate.ToString("t"));
    Console.WriteLine(msgLongTime             +         thisDate.ToString("T"));
    Console.WriteLine(msgFullDateShortTime    +         thisDate.ToString("f"));
    Console.WriteLine(msgFullDateLongTime     +         thisDate.ToString("F"));
    Console.WriteLine(msgGeneralDateShortTime +         thisDate.ToString("g"));
    Console.WriteLine(msgGeneralDateLongTime  +         thisDate.ToString("G"));
    Console.WriteLine(msgMonth                +         thisDate.ToString("M"));
    Console.WriteLine(msgRFC1123              +          utcDate.ToString("R"));
    Console.WriteLine(msgSortable             +         thisDate.ToString("s"));
    Console.WriteLine(msgUniSortInvariant     +          utcDate.ToString("u"));
    Console.WriteLine(msgUniFull              +         thisDate.ToString("U"));
    Console.WriteLine(msgYear                 +         thisDate.ToString("Y"));
    Console.WriteLine(msgRoundtripLocal       +         thisDate.ToString("o"));
    Console.WriteLine(msgRoundtripUTC         +          utcDate.ToString("o"));
    Console.WriteLine(msgRoundtripUnspecified +  unspecifiedDate.ToString("o"));

    Console.WriteLine();

// Display the same values using a CultureInfo object. The CultureInfo class 
// implements IFormatProvider.
    Console.WriteLine(msg2);

// Display the culture used to format the values. 
    ci = new CultureInfo("de-DE");
    Console.WriteLine("{0,-30}{1}\n", msgCulture, ci.DisplayName);

    Console.WriteLine(msgShortDate            +         thisDate.ToString("d", ci));
    Console.WriteLine(msgLongDate             +         thisDate.ToString("D", ci));
    Console.WriteLine(msgShortTime            +         thisDate.ToString("t", ci));
    Console.WriteLine(msgLongTime             +         thisDate.ToString("T", ci));
    Console.WriteLine(msgFullDateShortTime    +         thisDate.ToString("f", ci));
    Console.WriteLine(msgFullDateLongTime     +         thisDate.ToString("F", ci));
    Console.WriteLine(msgGeneralDateShortTime +         thisDate.ToString("g", ci));
    Console.WriteLine(msgGeneralDateLongTime  +         thisDate.ToString("G", ci));
    Console.WriteLine(msgMonth                +         thisDate.ToString("M", ci));
    Console.WriteLine(msgRFC1123              +         utcDate.ToString("R", ci));
    Console.WriteLine(msgSortable             +         thisDate.ToString("s", ci));
    Console.WriteLine(msgUniSortInvariant     +         utcDate.ToString("u", ci));
    Console.WriteLine(msgUniFull              +         thisDate.ToString("U", ci));
    Console.WriteLine(msgYear                 +         thisDate.ToString("Y", ci));
    Console.WriteLine(msgRoundtripLocal       +         thisDate.ToString("o", ci));
    Console.WriteLine(msgRoundtripUTC         +          utcDate.ToString("o", ci));
    Console.WriteLine(msgRoundtripUnspecified +  unspecifiedDate.ToString("o", ci));

    Console.WriteLine();
    }
}
/*
This code example produces the following results:

Standard DateTime Format Specifiers:

This date and time: 4/17/2006 2:22:48 PM

Use ToString(String) and the current thread culture.

Culture:                      English (United States)

(d) Short date: . . . . . . . 4/17/2006
(D) Long date:. . . . . . . . Monday, April 17, 2006
(t) Short time: . . . . . . . 2:22 PM
(T) Long time:. . . . . . . . 2:22:48 PM
(f) Full date/short time: . . Monday, April 17, 2006 2:22 PM
(F) Full date/long time:. . . Monday, April 17, 2006 2:22:48 PM
(g) General date/short time:. 4/17/2006 2:22 PM
(G) General date/long time (default):
    . . . . . . . . . . . . . 4/17/2006 2:22:48 PM
(M) Month:. . . . . . . . . . April 17
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:22:48 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:22:48
(u) Universal sortable (invariant):
    . . . . . . . . . . . . . 2006-04-17 21:22:48Z
(U) Universal full date/time: Monday, April 17, 2006 9:22:48 PM
(Y) Year: . . . . . . . . . . April, 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000

Use ToString(String, IFormatProvider) and a specified culture.

Culture:                      German (Germany)

(d) Short date: . . . . . . . 17.04.2006
(D) Long date:. . . . . . . . Montag, 17. April 2006
(t) Short time: . . . . . . . 14:22
(T) Long time:. . . . . . . . 14:22:48
(f) Full date/short time: . . Montag, 17. April 2006 14:22
(F) Full date/long time:. . . Montag, 17. April 2006 14:22:48
(g) General date/short time:. 17.04.2006 14:22
(G) General date/long time (default):
    . . . . . . . . . . . . . 17.04.2006 14:22:48
(M) Month:. . . . . . . . . . 17 April
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:22:48 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:22:48
(u) Universal sortable (invariant):
    . . . . . . . . . . . . . 2006-04-17 21:22:48Z
(U) Universal full date/time: Montag, 17. April 2006 21:22:48
(Y) Year: . . . . . . . . . . April 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000

*/
// This code example demonstrates the ToString(String) and 
// ToString(String, IFormatProvider) methods for the DateTime 
// type in conjunction with the standard date and time 
// format specifiers.

using namespace System;
using namespace System::Globalization;
using namespace System::Threading;

    int main() 
    {
    String^ msgShortDate = "(d) Short date: . . . . . . . ";
    String^ msgLongDate  = "(D) Long date:. . . . . . . . ";
    String^ msgShortTime = "(t) Short time: . . . . . . . ";
    String^ msgLongTime  = "(T) Long time:. . . . . . . . ";
    String^ msgFullDateShortTime = 
                          "(f) Full date/short time: . . ";
    String^ msgFullDateLongTime =
                          "(F) Full date/long time:. . . ";
    String^ msgGeneralDateShortTime = 
                          "(g) General date/short time:. ";
    String^ msgGeneralDateLongTime = 
                          "(G) General date/long time (default):\n" +
                          "    . . . . . . . . . . . . . ";
    String^ msgMonth   =   "(M) Month:. . . . . . . . . . ";
    String^ msgRFC1123 =   "(R) RFC1123:. . . . . . . . . ";
    String^ msgSortable =  "(s) Sortable: . . . . . . . . ";
    String^ msgUniSortInvariant = 
                          "(u) Universal sortable (invariant):\n" + 
                          "    . . . . . . . . . . . . . ";
    String^ msgUniFull =   "(U) Universal full date/time: ";
    String^ msgYear =      "(Y) Year: . . . . . . . . . . ";
    String^ msgRoundtripLocal        = "(o) Roundtrip (local):. . . . ";
    String^ msgRoundtripUTC          = "(o) Roundtrip (UTC):. . . . . ";
    String^ msgRoundtripUnspecified  = "(o) Roundtrip (Unspecified):. ";

    String^ msg1 = "Use ToString(String) and the current thread culture.\n";
    String^ msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n";
    String^ msgCulture  = "Culture:";
    String^ msgThisDate = "This date and time: {0}\n";

    DateTime^ thisDate  = DateTime::Now;
    DateTime^  utcDate  = thisDate->ToUniversalTime();
    DateTime^ unspecifiedDate = gcnew DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind::Unspecified);
    CultureInfo^ ci;

// Format the current date and time in various ways.
    Console::Clear();
    Console::WriteLine("Standard DateTime Format Specifiers:\n");
    Console::WriteLine(msgThisDate, thisDate);
    Console::WriteLine(msg1);

// Display the thread current culture, which is used to format the values.
    ci = Thread::CurrentThread->CurrentCulture;
    Console::WriteLine("{0,-30}{1}\n", msgCulture, ci->DisplayName);

    Console::WriteLine(msgShortDate            +        thisDate->ToString("d"));
    Console::WriteLine(msgLongDate             +        thisDate->ToString("D"));
    Console::WriteLine(msgShortTime            +        thisDate->ToString("t"));
    Console::WriteLine(msgLongTime             +        thisDate->ToString("T"));
    Console::WriteLine(msgFullDateShortTime    +        thisDate->ToString("f"));
    Console::WriteLine(msgFullDateLongTime     +        thisDate->ToString("F"));
    Console::WriteLine(msgGeneralDateShortTime +        thisDate->ToString("g"));
    Console::WriteLine(msgGeneralDateLongTime  +        thisDate->ToString("G"));
    Console::WriteLine(msgMonth                +        thisDate->ToString("M"));
    Console::WriteLine(msgRFC1123              +         utcDate->ToString("R"));
    Console::WriteLine(msgSortable             +        thisDate->ToString("s"));
    Console::WriteLine(msgUniSortInvariant     +         utcDate->ToString("u"));
    Console::WriteLine(msgUniFull              +        thisDate->ToString("U"));
    Console::WriteLine(msgYear                 +        thisDate->ToString("Y"));
    Console::WriteLine(msgRoundtripLocal       +        thisDate->ToString("o"));
    Console::WriteLine(msgRoundtripUTC         +         utcDate->ToString("o"));
    Console::WriteLine(msgRoundtripUnspecified + unspecifiedDate->ToString("o"));
    Console::WriteLine();

// Display the same values using a CultureInfo object. The CultureInfo class 
// implements IFormatProvider.
    Console::WriteLine(msg2);

// Display the culture used to format the values. 
    ci = gcnew CultureInfo("de-DE");
    Console::WriteLine("{0,-30}{1}\n", msgCulture, ci->DisplayName);

    Console::WriteLine(msgShortDate            +        thisDate->ToString("d", ci));
    Console::WriteLine(msgLongDate             +        thisDate->ToString("D", ci));
    Console::WriteLine(msgShortTime            +        thisDate->ToString("t", ci));
    Console::WriteLine(msgLongTime             +        thisDate->ToString("T", ci));
    Console::WriteLine(msgFullDateShortTime    +        thisDate->ToString("f", ci));
    Console::WriteLine(msgFullDateLongTime     +        thisDate->ToString("F", ci));
    Console::WriteLine(msgGeneralDateShortTime +        thisDate->ToString("g", ci));
    Console::WriteLine(msgGeneralDateLongTime  +        thisDate->ToString("G", ci));
    Console::WriteLine(msgMonth                +        thisDate->ToString("M", ci));
    Console::WriteLine(msgRFC1123              +         utcDate->ToString("R", ci));
    Console::WriteLine(msgSortable             +        thisDate->ToString("s", ci));
    Console::WriteLine(msgUniSortInvariant     +         utcDate->ToString("u", ci));
    Console::WriteLine(msgUniFull              +        thisDate->ToString("U", ci));
    Console::WriteLine(msgYear                 +        thisDate->ToString("Y", ci));
    Console::WriteLine(msgRoundtripLocal       +        thisDate->ToString("o", ci));
    Console::WriteLine(msgRoundtripUTC         +         utcDate->ToString("o", ci));
    Console::WriteLine(msgRoundtripUnspecified + unspecifiedDate->ToString("o", ci));
    Console::WriteLine();
    }

/*
This code example produces the following results:

Standard DateTime Format Specifiers:

This date and time: 4/17/2006 2:38:09 PM

Use ToString(String) and the current thread culture.

Culture:                      English (United States)

(d) Short date: . . . . . . . 4/17/2006
(D) Long date:. . . . . . . . Monday, April 17, 2006
(t) Short time: . . . . . . . 2:38 PM
(T) Long time:. . . . . . . . 2:38:09 PM
(f) Full date/short time: . . Monday, April 17, 2006 2:38 PM
(F) Full date/long time:. . . Monday, April 17, 2006 2:38:09 PM
(g) General date/short time:. 4/17/2006 2:38 PM
(G) General date/long time (default):
    . . . . . . . . . . . . . 4/17/2006 2:38:09 PM
(M) Month:. . . . . . . . . . April 17
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:38:09 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:38:09
(u) Universal sortable (invariant):
    . . . . . . . . . . . . . 2006-04-17 21:38:09Z
(U) Universal full date/time: Monday, April 17, 2006 9:38:09 PM
(Y) Year: . . . . . . . . . . April, 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:38:09.9417500-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:38:09.9417500Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000

Use ToString(String, IFormatProvider) and a specified culture.

Culture:                      German (Germany)

(d) Short date: . . . . . . . 17.04.2006
(D) Long date:. . . . . . . . Montag, 17. April 2006
(t) Short time: . . . . . . . 14:38
(T) Long time:. . . . . . . . 14:38:09
(f) Full date/short time: . . Montag, 17. April 2006 14:38
(F) Full date/long time:. . . Montag, 17. April 2006 14:38:09
(g) General date/short time:. 17.04.2006 14:38
(G) General date/long time (default):
    . . . . . . . . . . . . . 17.04.2006 14:38:09
(M) Month:. . . . . . . . . . 17 April
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:38:09 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:38:09
(u) Universal sortable (invariant):
    . . . . . . . . . . . . . 2006-04-17 21:38:09Z
(U) Universal full date/time: Montag, 17. April 2006 21:38:09
(Y) Year: . . . . . . . . . . April 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:38:09.9417500-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:38:09.9417500Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000

*/

참고 항목

개념

형식 지정 개요

날짜 및 시간 형식 문자열

사용자 지정 날짜 및 시간 형식 문자열

기타 리소스

형식 지정