DateTimeFormatter クラス

定義

日付と時刻を書式設定します。

public ref class DateTimeFormatter sealed
/// [Windows.Foundation.Metadata.Activatable(Windows.Globalization.DateTimeFormatting.IDateTimeFormatterFactory, 65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class DateTimeFormatter final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.Activatable(Windows.Globalization.DateTimeFormatting.IDateTimeFormatterFactory, 65536, "Windows.Foundation.UniversalApiContract")]
class DateTimeFormatter final
[Windows.Foundation.Metadata.Activatable(typeof(Windows.Globalization.DateTimeFormatting.IDateTimeFormatterFactory), 65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class DateTimeFormatter
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.Activatable(typeof(Windows.Globalization.DateTimeFormatting.IDateTimeFormatterFactory), 65536, "Windows.Foundation.UniversalApiContract")]
public sealed class DateTimeFormatter
function DateTimeFormatter(yearFormat, monthFormat, dayFormat, dayOfWeekFormat, hourFormat, minuteFormat, secondFormat, languages, geographicRegion, calendar, clock)
Public NotInheritable Class DateTimeFormatter
継承
Object Platform::Object IInspectable DateTimeFormatter
属性

Windows の要件

デバイス ファミリ
Windows 10 (10.0.10240.0 で導入)
API contract
Windows.Foundation.UniversalApiContract (v1.0 で導入)

DateTimeFormatter クラスを使用して、日付の書式を設定します。

// Format a date via a string template. Note that the order specifed in the string pattern does
// not determine the order of the parts of the formatted string. The user's language and region
// preferences will determine the pattern of the date returned based on the specified parts.

var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("month day dayofweek year");
DateTime dateToFormat = DateTime.Now;
var mydate = formatter.Format(dateToFormat);

パターン文法次の文法では、パターンに対して受け入れられる内容を定義します。

<pattern> ::= [<literal-text>] <datetime-pattern> [<literal-text>] |
              [<literal-text>] <datetime-pattern> <pattern>

<literal-text> ::= <literal-character>+

<literal-character> ::= [^{}] | "{openbrace}" | "{closebrace}"

<datetime-pattern> ::= <date-pattern> | <time-pattern>

<date-pattern> ::= <era> | <year> | <month> | <day> | <dayofweek>

<time-pattern> ::= <period> | <hour> | <minute> | <second> | <timezone>

<era> ::= "{era.abbreviated" [<ideal-length>] "}"

<year> ::= "{year.full" [<ideal-length>] "}" |
           "{year.abbreviated" [<ideal-length>] "}"

<month> ::= "{month.full}" |
            "{month.solo.full}" |
            "{month.abbreviated" [<ideal-length>] "}"
            "{month.solo.abbreviated" [<ideal-length>] "}"
            "{month.integer" [<ideal-length>] "}"

<dayofweek> ::= "{dayofweek.full}" |
                "{dayofweek.solo.full}" |
                "{dayofweek.abbreviated" [<ideal-length>] "}"
                "{dayofweek.solo.abbreviated" [<ideal-length>] "}"

<day> ::= "{day.integer" [<ideal-length>] "}"

<period> ::= "{period.abbreviated" [<ideal-length>] "}"

<hour> ::= "{hour.integer" [<ideal-length>] "}"

<minute> ::= "{minute.integer" [<ideal-length>] "}"

<second> ::= "{second.integer" [<ideal-length>] "}"

<timezone> ::= "{timezone.full}" |
                "{timezone.abbreviated" [<ideal-length>] "}"

<ideal-length> ::= "(" <non-zero-digit> ")"

<non-zero-digit> ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

テンプレート文法次の文法では、テンプレートに対して受け入れられる内容を定義します。

<template> ::= <opt-whitespace> <date> <opt-whitespace> |
               <opt-whitespace> <time> <opt-whitespace> |
               <opt-whitespace> <specific-date> <whitespace> <time> <opt-whitespace> |
               <opt-whitespace> <time> <whitespace> <specific-date> <opt-whitespace> |
               <opt-whitespace> <relative-date> <whitespace> <time> <opt-whitespace> |
               <opt-whitespace> <time> <whitespace> <relative-date> <opt-whitespace>

<opt-whitespace> ::= [<whitespace>] 

<whitespace> ::= " "+ 

<date> ::= <year> | <month> | <day> | <month-year> | <relative-date> | <specific-date>

<relativedate> ::= <dayofweek> | <month-day> | <relative-longdate>

<specificdate> ::= <shortdate> | <longdate>

<month-day> ::= <month> <whitespace> <day> |
                <day> <whitespace> <month>

<relative-longdate> ::= <month> <whitespace> <day> <whitespace> <dayofweek> |
                        <month> <whitespace> <dayofweek> <whitespace> <day> |
                        <day> <whitespace> <month> <whitespace> <dayofweek> |
                        <day> <whitespace> <dayofweek> <whitespace> <month> |
                        <dayofweek> <whitespace> <day> <whitespace> <month> |
                        <dayofweek> <whitespace> <month> <whitespace> <day>

<month-year> ::= <month> <whitespace> <year> |
                 <year> <whitespace> <month>


<shortdate> ::= "shortdate" |
                <month> <whitespace> <day> <whitespace> <year> |
                <month> <whitespace> <year> <whitespace> <day> |
                <day> <whitespace> <month> <whitespace> <year> |
                <day> <whitespace> <year> <whitespace> <month> |
                <year> <whitespace> <day> <whitespace> <month> |
                <year> <whitespace> <month> <whitespace> <day>


<longdate> ::= "longdate" |
               <year> <whitespace> <month> <whitespace> <day> <whitespace> <dayofweek> |
               <year> <whitespace> <month> <whitespace> <dayofweek> <whitespace> <day> |
               <year> <whitespace> <day> <whitespace> <month> <whitespace> <dayofweek> |
               <year> <whitespace> <day> <whitespace> <dayofweek> <whitespace> <month> |
               <year> <whitespace> <dayofweek> <whitespace> <day> <whitespace> <month> |
               <year> <whitespace> <dayofweek> <whitespace> <month> <whitespace> <day> |
               <month> <whitespace> <year> <whitespace> <day> <whitespace> <dayofweek> |
               <month> <whitespace> <year> <whitespace> <dayofweek> <whitespace> <day> |
               <day> <whitespace> <year> <whitespace> <month> <whitespace> <dayofweek> |
               <day> <whitespace> <year> <whitespace> <dayofweek> <whitespace> <month> |
               <dayofweek> <whitespace> <year> <whitespace> <day> <whitespace> <month> |
               <dayofweek> <whitespace> <year> <whitespace> <month> <whitespace> <day> |
               <month> <whitespace> <day> <whitespace> <year> <whitespace> <dayofweek> |
               <month> <whitespace> <dayofweek> <whitespace> <year> <whitespace> <day> |
               <day> <whitespace> <month> <whitespace> <year> <whitespace> <dayofweek> |
               <day> <whitespace> <dayofweek> <whitespace> <year> <whitespace> <month> |
               <dayofweek> <whitespace> <day> <whitespace> <year> <whitespace> <month> |
               <dayofweek> <whitespace> <month> <whitespace> <year> <whitespace> <day> |
               <month> <whitespace> <day> <whitespace> <dayofweek> <whitespace> <year> |
               <month> <whitespace> <dayofweek> <whitespace> <day> <whitespace> <year> |
               <day> <whitespace> <month> <whitespace> <dayofweek> <whitespace> <year> |
               <day> <whitespace> <dayofweek> <whitespace> <month> <whitespace> <year> |
               <dayofweek> <whitespace> <day> <whitespace> <month> <whitespace> <year> |
               <dayofweek> <whitespace> <month> <whitespace> <day> <whitespace> <year>

<time> ::= <hour> | 
           <hour> <whitespace> <timezone> |
           <timezone> <whitespace> <hour> |
           <shorttime> |
           <longtime>

<shorttime> ::= "shorttime" |
                <hour> <whitespace> <minute> |
                <minute> <whitespace> <hour> |
                <timezone> <whitespace> <hour> <whitespace> <minute> |
                <timezone> <whitespace> <minute> <whitespace> <hour> |
                <hour> <whitespace> <timezone> <whitespace> <minute> |
                <minute> <whitespace> <timezone> <whitespace> <hour> |
                <hour> <whitespace> <minute> <whitespace> <timezone> |
                <minute> <whitespace> <hour> <whitespace> <timezone>

<longtime> ::= "longtime" |
               <hour> <whitespace> <minute> <whitespace> <second> |
               <hour> <whitespace> <second> <whitespace> <minute> |
               <minute> <whitespace> <hour> <whitespace> <second> |
               <minute> <whitespace> <second> <whitespace> <hour> |
               <second> <whitespace> <minute> <whitespace> <hour> |
               <second> <whitespace> <hour> <whitespace> <minute> |
               <timezone> <whitespace> <hour> <whitespace> <minute> <whitespace> <second> |
               <timezone> <whitespace> <hour> <whitespace> <second> <whitespace> <minute> |
               <timezone> <whitespace> <minute> <whitespace> <hour> <whitespace> <second> |
               <timezone> <whitespace> <minute> <whitespace> <second> <whitespace> <hour> |
               <timezone> <whitespace> <second> <whitespace> <minute> <whitespace> <hour> |
               <timezone> <whitespace> <second> <whitespace> <hour> <whitespace> <minute> |
               <hour> <whitespace> <timezone> <whitespace> <minute> <whitespace> <second> |
               <hour> <whitespace> <timezone> <whitespace> <second> <whitespace> <minute> |
               <minute> <whitespace> <timezone> <whitespace> <hour> <whitespace> <second> |
               <minute> <whitespace> <timezone> <whitespace> <second> <whitespace> <hour> |
               <second> <whitespace> <timezone> <whitespace> <minute> <whitespace> <hour> |
               <second> <whitespace> <timezone> <whitespace> <hour> <whitespace> <minute> |
               <hour> <whitespace> <minute> <whitespace> <timezone> <whitespace> <second> |
               <hour> <whitespace> <second> <whitespace> <timezone> <whitespace> <minute> |
               <minute> <whitespace> <hour> <whitespace> <timezone> <whitespace> <second> |
               <minute> <whitespace> <second> <whitespace> <timezone> <whitespace> <hour> |
               <second> <whitespace> <minute> <whitespace> <timezone> <whitespace> <hour> |
               <second> <whitespace> <hour> <whitespace> <timezone> <whitespace> <minute> |
               <hour> <whitespace> <minute> <whitespace> <second> <whitespace> <timezone> |
               <hour> <whitespace> <second> <whitespace> <minute> <whitespace> <timezone> |
               <minute> <whitespace> <hour> <whitespace> <second> <whitespace> <timezone> |
               <minute> <whitespace> <second> <whitespace> <hour> <whitespace> <timezone> |
               <second> <whitespace> <minute> <whitespace> <hour> <whitespace> <timezone> |
               <second> <whitespace> <hour> <whitespace> <minute> <whitespace> <timezone>

<year> ::= "year" | "year.full" | "year.abbreviated"

<month> ::= "month" | "month.full" | "month.abbreviated" | "month.numeric"

<day> ::= "day"

<dayofweek> ::= "dayofweek" | "dayofweek.full" | "dayofweek.abbreviated"

<hour> ::= "hour"

<minute> ::= "minute"

<second> ::= "second"

<timezone> ::= "timezone" | "timezone.full" | "timezone.abbreviated"

注釈

Note

この API は、2019 年 5 月の日本の時代変更をサポートするように更新されています。 アプリケーションで日本語カレンダーがサポートされている場合は、新しい時代 (年号) が適切に処理されていることを検証する必要があります。 詳細については、「 日本の時代 (年号) の変更に合わせてアプリケーションを準備 する」を参照してください。

Note

アプリがこのクラスで使用されている言語タグを 各国語サポート 関数に渡す場合は、最初に ResolveLocaleName を呼び出してタグを変換する必要があります。

DateTimeFormatter の一部のコンストラクターは、書式設定する要求されたコンポーネントを指定する formatTemplate 文字列パラメーターを受け取ります。 このパラメーターには、 書式テンプレート または 書式パターンを指定できます。 書式テンプレートは、指定した言語、時計、カレンダーに一般的な書式設定を使用して提供するのに便利です。 書式パターンを使用すると、書式テンプレートよりも出力を制御できます。 標準以外の書式設定が必要な場合は、テンプレートの代わりに書式パターンを使用します。

書式テンプレートでは、書式設定された出力に含めるコンポーネント (時刻、日付、曜日など) を指定します。 フォーマッタは、指定した言語、時計、カレンダーに合わせてコンポーネントを書式設定します。 テンプレート内のコンポーネントの順序は関係ありません。フォーマッタは、以下の書式テンプレート言語に示されている順序に従ってコンポーネントを並べ替えます。

一般的に使用されるテンプレートの一部を次の表に示します。 複数のコンポーネントを含むテンプレートでは、コンポーネントはサポートされているコンポーネントのバリエーションのいずれかであり、コンポーネントは任意の順序で使用できます。 たとえば、"dayofweek.full year month.numeric day" がサポートされています。 (有効なテンプレートを定義する文法については、次の例の後を参照してください)。

重要

コンポーネントを任意に組み合わせて、必ずしも有効なテンプレートを取得することはできません。 有効なテンプレートは、指定された文法で定義されているテンプレートのみです。

Template
longdate
shortdate
長年
短期間
dayofweek
dayofweek.full
dayofweek.abbreviated
day
month
month.full
month.abbreviated
month.numeric
year
year.full
year.abbreviated
dayofweek day month year
dayofweek 日月
日の月の年
日月
月/年
hour
minute
second
hour minute second
時間 (分)
timezone
timezone.full
timezone.abbreviated
年月の日の時間

次の表は、テンプレート、言語、クロックのさまざまな値を使用して作成されたフォーマッタからの出力例を示しています。 [同等のパターン] 列には、テンプレートと同じ出力を生成するパターンが用意されています。

テンプレートLanguage時計出力同等のパターン
longdateja-JP<指定されていません>2013 年 9 月 2 日 (月){dayofweek.full}、{month.full} {day.integer}、{year.full}
longdatefr-FR<指定されていません>lundi 2 septembre 2013{dayofweek.full} {day.integer} {month.full} {year.full}
shortdateja-JP<指定されていません>9‎/‎2‎/‎2013{month.integer}/{day.integer}/{year.full}
shortdatefr-FR<指定されていません>02‎/‎09‎/‎2013{day.integer(2)}/{month.integer(2)}/{year.full}
dayofweek month dayja-JP<指定されていません>9 月 2 日 (月){dayofweek.full}、{month.full} {day.integer}
month day dayofweekja-JP<指定されていません>9 月 2 日 (月){dayofweek.full}、{month.full} {day.integer}
長年ja-JP<指定されていません>6:01:24 AM{hour.integer}:{minute.integer(2)}:{second.integer(2)} {period.abbreviated}
長年ja-JP24HourClock‎17‎:‎32‎:‎45{hour.integer}:{minute.integer(2)}:{second.integer(2)}
長年fr-FR<指定されていません>17‎:‎32‎:‎45{hour.integer(2)}:{minute.integer(2)}:{second.integer(2)}
短期間ja-JP<指定されていません>午前 6:01{hour.integer}:{minute.integer(2)} {period.abbreviated}
短期間ja-JP24HourClock17‎:‎32{hour.integer}:{minute.integer(2)}
短期間fr-FR<指定されていません>17‎:‎32{hour.integer(2)}:{minute.integer(2)}

書式パターン は、"{day.numeric}/{month.numeric}/{year.full}" などの中かっこで区切られたフィールドを持つリテラル文字列で構成されます。 一般的に使用されるフィールドを次の表に示します。 (有効なパターンを定義する文法については、次の例の後を参照してください)。

重要

フィールドを任意に結合して、必ずしも有効なパターンを取得することはできません。 唯一の有効なパターンは、指定された文法で定義されているパターンです。

書式パターン フィールド
{openbrace}
{closebrace}
{dayofweek.solo.<type>} ( <type が完全/省略形/省略形(n) の場合> )
{dayofweek。<type>} ( <type が完全/省略形/省略形(n) の場合> )
{day.<type>} (type> が integer/ integer(n) である場合<
{month。<type>} where <type> is full / abbreviated / abbreviated(n) / integer / integer(n)
{month.solo.<type>} ( <type が完全/省略形/省略形(n) の場合> )
{year。<type>} ( <type が完全/省略形/省略形(n) の場合> )
{era。<type>} ( <type が完全/省略形/省略形(n) の場合> )
{hour。<type>} (type> が integer/ integer(n) である場合<
{period.<type>} ( <type が完全/省略形/省略形(n) の場合> )
{minute。<type>} (type> が integer/ integer(n) である場合<
{second.<type>} (type> が integer/ integer(n) である場合<
{タイムゾーン。<type>} ( <type が完全/省略形/省略形(n) の場合> )

Note

  • {openbrace} または {closebrace} を使用して、リテラル文字列の一部として "{" または "}" が表示されるように指定します。
  • solo はスタンドアロン フォームを指定します。 生成的な大文字と小文字の区別を持つ言語の場合、生成的なケースはマークされていないケースです。 指名ケースを取得するには、solo を使用 します
  • 月の 5 日目の integer(2) は "05" で、integer(1) は "5" を生成します。 integer は、特定の言語/カレンダーの組み合わせに最適な n を生成します。
  • 年の場合、 full はすべての数字 ("2013") を生成します。 abbreviated は、グレゴリオ暦 ("13") の 2 桁の年である abbreviated(2) と同義です。 (他のカレンダーでは、最終的に既定値として他のものを使用する場合があります。特定の言語とカレンダーの組み合わせに最適なセマンティクスを持つように 省略されています )。 abbreviated(n) は、最下位 の n 桁に切り捨てられます (たとえば、 n =1 の 2013 は "3" です)。

次の表は、パターン、言語、およびクロックのさまざまな値を使用して作成されたフォーマッタからの出力例を示しています。

Pattern言語時計出力
{dayofweek.full}、{month.full} {day.integer}、{year.full}ja-JP<指定されていません>2013 年 9 月 2 日 (月)
{dayofweek.full}、{day.integer(2)} {month.full}、{year.full}fr-FR<指定されていません>lundi、2013 年 2 月 2 日
{month.integer}/{day.integer}/{year.full}ja-JP<指定されていません>9/‎2/2013
{month.integer}/{day.integer}/{year.abbreviated}ja-JP<指定されていません>9/‎2/13
{month.integer(2)}/{day.integer(2)}/{year.abbreviated}ja-JP<指定されていません>09/‎02/13
{hour.integer}:{minute.integer(2)}:{second.integer(2)} {period.abbreviated}ja-JP<指定されていません>6:01:24 AM
{hour.integer}:{minute.integer(2)}:{second.integer(2)} {period.abbreviated}ja-JP24HourClock17:32:45

コンストラクター

DateTimeFormatter(HourFormat, MinuteFormat, SecondFormat)

時間、分、および 2 番目の形式で初期化される DateTimeFormatter オブジェクトを作成します。

DateTimeFormatter(String)

書式テンプレート文字列によって初期化される DateTimeFormatter オブジェクトを作成します。

DateTimeFormatter(String, IIterable<String>)

書式テンプレートの文字列と言語リストによって初期化される DateTimeFormatter オブジェクトを作成します。

DateTimeFormatter(String, IIterable<String>, String, String, String)

書式テンプレート文字列、言語リスト、地域、カレンダー、時計によって初期化される DateTimeFormatter オブジェクトを作成します。

DateTimeFormatter(YearFormat, MonthFormat, DayFormat, DayOfWeekFormat)

年、月、日、曜日の形式で初期化される DateTimeFormatter オブジェクトを作成します。

DateTimeFormatter(YearFormat, MonthFormat, DayFormat, DayOfWeekFormat, HourFormat, MinuteFormat, SecondFormat, IIterable<String>)

年、月、日、曜日、時間、分、および 2 番目の書式、および言語リストで初期化される DateTimeFormatter オブジェクトを作成します。

DateTimeFormatter(YearFormat, MonthFormat, DayFormat, DayOfWeekFormat, HourFormat, MinuteFormat, SecondFormat, IIterable<String>, String, String, String)

指定されたすべての形式と識別子を持つ DateTimeFormatter オブジェクトを作成します。

プロパティ

Calendar

日付の書式設定時に使用されるカレンダーを取得します。

Clock

時刻の書式設定時に使用されるクロックを取得します。

GeographicRegion

日付と時刻を書式設定するときに使用される領域を取得または設定します。

IncludeDay

テンプレート内の DayFormat を取得します。

IncludeDayOfWeek

テンプレート内の DayOfWeekFormat を取得します。

IncludeHour

テンプレート内の HourFormat を取得します。

IncludeMinute

テンプレート内の MinuteFormat を取得します。

IncludeMonth

テンプレートの MonthFormat を取得します。

IncludeSecond

テンプレート内の SecondFormat を取得します。

IncludeYear

テンプレートの YearFormat を取得します。

Languages

日付と時刻を書式設定するときに使用される言語識別子の優先順位リストを取得します。

LongDate

ユーザーが選択した長い日付パターンに従って日付を書式設定する DateTimeFormatter オブジェクトを取得します。

LongTime

ユーザーが選択した長い時間パターンに従って時刻を書式設定する DateTimeFormatter オブジェクトを取得します。

NumeralSystem

日付と時刻の書式を設定するために使用される番号付けシステムを取得または設定します。

Patterns

日付と時刻を書式設定するときに使用されるこのテンプレートに対応するパターンを取得します。

ResolvedGeographicRegion

日付と時刻の書式設定に最後に使用された地理的リージョンを取得します。

ResolvedLanguage

日付と時刻の書式設定に最後に使用された言語を取得します。

ShortDate

ユーザーが選択した短い日付パターンに従って日付を書式設定する DateTimeFormatter オブジェクトを取得します。

ShortTime

ユーザーが選択した短い時間パターンに従って時刻を書式設定する DateTimeFormatter オブジェクトを取得します。

Template

この書式テンプレートの文字列表現を取得します。

メソッド

Format(DateTime)

指定された日付と時刻の文字列形式を返します。

Format(DateTime, String)

フォーマッタが現在使用しているテンプレートに準拠して、指定されたタイム ゾーン内の指定された日付と時刻を表す文字列を返します。

適用対象

こちらもご覧ください