標準の DateTime 書式指定文字列

標準の DateTime 書式指定文字列は、単一の標準 DateTime 書式指定子文字で構成されます。この書式指定子文字は、カスタム DateTime 書式指定文字列を表します。書式指定文字列が最終的に定義するのは、書式設定操作によって作成される DateTime オブジェクトのテキスト表現です。空白を含む複数の英文字で構成される DateTime 書式指定文字列は、カスタム DateTime 書式指定文字列として解釈されるので注意してください。

標準の DateTime 書式指定子

標準の DateTime 書式指定子を次の表に示します。各書式指定子によって生成される出力の例については、「標準の DateTime 書式指定文字列の出力例」を参照してください。

書式指定子 名前 説明

d

短い形式の日付パターン

現在の ShortDatePattern プロパティで定義されているカスタム DateTime 書式指定文字列を表します。

たとえば、インバリアント カルチャのカスタム書式指定文字列は "MM/dd/yyyy" です。

D

長い形式の日付パターン

現在の LongDatePattern プロパティで定義されているカスタム DateTime 書式指定文字列を表します。

たとえば、インバリアント カルチャのカスタム書式指定文字列は "dddd, dd MMMM yyyy" です。

f

完全な日付と時刻のパターン (短い形式の時刻)

長い形式の日付 (D) パターンと短い形式の時刻 (t) パターンを空白で区切って組み合わせて表します。

F

完全な日付と時刻のパターン (長い形式の時刻)

現在の FullDateTimePattern プロパティで定義されているカスタム DateTime 書式指定文字列を表します。

たとえば、インバリアント カルチャのカスタム書式指定文字列は "dddd, dd MMMM yyyy HH:mm:ss" です。

g

一般の日付と時刻のパターン (短い形式の時刻)

短い形式の日付 (d) パターンと短い形式の時刻 (t) パターンを空白で区切って組み合わせて表します。

G

一般の日付と時刻のパターン (長い形式の時刻)

短い形式の日付 (d) パターンと長い形式の時刻 (T) パターンを空白で区切って組み合わせて表します。

M または m

月日パターン

現在の MonthDayPattern プロパティで定義されているカスタム DateTime 書式指定文字列を表します。

たとえば、インバリアント カルチャのカスタム書式指定文字列は "MMMM dd" です。

o

ラウンド トリップする日付と時刻のパターン

タイム ゾーン情報を保持するパターンを使用するカスタム DateTime 書式指定文字列を表します。このパターンは、Kind プロパティを含めた DateTime の形式をテキストでラウンド トリップするように意図されています。書式設定された文字列は、Parse または ParseExact を正しい Kind プロパティ値を指定して変換前の文字列に戻ることができます。

カスタム書式指定文字列は、"yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK" です。

この指定子のパターンは、定義済みの標準です。したがって、使用されるカルチャまたは指定された書式プロバイダに関係なく、常に同じです。

R または r

RFC1123 パターン

現在の RFC1123Pattern プロパティで定義されているカスタム DateTime 書式指定文字列を表します。このパターンは定義済みの標準で、プロパティは読み取り専用です。したがって、使用されるカルチャまたは指定された書式プロバイダに関係なく、常に同じです。

カスタム書式指定文字列は、"ddd, dd MMM yyyy HH':'mm':'ss 'GMT'" です。

書式を設定しても、書式設定される DateTime オブジェクトの値は変更されません。このため、アプリケーションでは、この書式指定子を使用する前に、値を世界協定時刻 (UTC) に変換する必要があります。

s

並べ替え可能な日付と時刻のパターン (ISO 8601 準拠)

現在の SortableDateTimePattern プロパティで定義されているカスタム DateTime 書式指定文字列を表します。このパターンは定義済みの標準で、プロパティは読み取り専用です。したがって、使用されるカルチャまたは指定された書式プロバイダに関係なく、常に同じです。

カスタム書式指定文字列は、"yyyy'-'MM'-'dd'T'HH':'mm':'ss" です。

t

短い形式の時刻パターン

現在の ShortTimePattern プロパティで定義されているカスタム DateTime 書式指定文字列を表します。

たとえば、インバリアント カルチャのカスタム書式指定文字列は "HH:mm" です。

T

長い形式の時刻パターン

現在の LongTimePattern プロパティで定義されているカスタム DateTime 書式指定文字列を表します。

たとえば、インバリアント カルチャのカスタム書式指定文字列は "HH:mm:ss" です。

u

並べ替え可能な日付と時刻のパターン (世界時刻)

現在の UniversalSortableDateTimePattern プロパティで定義されているカスタム DateTime 書式指定文字列を表します。このパターンは定義済みの標準で、プロパティは読み取り専用です。したがって、使用されるカルチャまたは指定された書式プロバイダに関係なく、常に同じです。

カスタム書式指定文字列は、"yyyy'-'MM'-'dd HH':'mm':'ss'Z'" です。

日付と時刻が書式設定されるときに、タイム ゾーン変換は行われません。このため、アプリケーションでは、この書式指定子を使用する前に、ローカルの日付と時刻を世界協定時刻 (UTC) に変換する必要があります。

U

並べ替え可能な日付と時刻のパターン (世界時刻)

現在の FullDateTimePattern プロパティで定義されているカスタム DateTime 書式指定文字列を表します。

このパターンは、完全な日付と時刻 (F) のパターンと同じです。ただし、書式設定は、書式設定される DateTime オブジェクトに相当する世界協定時刻 (UTC) に対して行われます。

Y または y

年月パターン

現在の YearMonthPattern プロパティで定義されているカスタム DateTime 書式指定文字列を表します。

たとえば、インバリアント カルチャのカスタム書式指定文字列は "yyyy MMMM" です。

その他の 1 文字

(未定義の指定子)

未定義の指定子は、ランタイム書式指定例外をスローします。

コントロール パネルの設定

コントロール パネルの [地域と言語のオプション] での設定は、書式設定操作によって生成される結果の文字列に影響します。これらの設定は、書式設定の制御に使用される値を提供する現在のスレッド カルチャに関連付けられた DateTimeFormatInfo オブジェクトを初期化するために使用されます。コンピュータで使用する設定が異なる場合は、生成される文字列も異なります。

DateTimeFormatInfo のプロパティ

書式設定は、現在の DateTimeFormatInfo オブジェクトのプロパティの影響を受けます。このオブジェクトは、現在のスレッド カルチャによって暗黙的に指定されるか、または書式設定を実行するメソッドの IFormatProvider パラメータによって明示的に指定されます。IFormatProvider パラメータには、カルチャを表す CultureInfo オブジェクト、または DateTimeFormatInfo オブジェクトを指定します。

標準の DateTime 書式指定子の多くは、現在の DateTimeFormatInfo オブジェクトのプロパティによって定義されている書式設定パターンのエイリアスです。このため、アプリケーションでは、一部の標準 DateTime 書式指定子で生成される結果を、対応する 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 書式指定子を使用して、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 msgUniSort As String   = "(U) Universal sortable: . . . "
        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(msgUniSort              &        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(msgUniSort              &        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 sortable: . . . 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 sortable: . . . 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 msgUniSort =   "(U) Universal sortable: . . . ";
    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(msgUniSort              +         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(msgUniSort              +         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 sortable: . . . 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 sortable: . . . 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^ msgUniSort =   "(U) Universal sortable: . . . ";
    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(msgUniSort              +        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(msgUniSort              +        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 sortable: . . . 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 sortable: . . . 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

*/

参照

概念

書式設定の概要
日付と時刻の書式指定文字列
標準の DateTime 書式指定文字列の出力例

その他の技術情報

型の書式設定