DateTimeFormatter class

1 out of 7 rated this helpful - Rate this topic

Formats dates and times.

Syntax


var dateTimeFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter(string);
var dateTimeFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter(string, iIterable(String);
var dateTimeFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter(string, iIterable(String);
var dateTimeFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter(yearFormat, monthFormat, dayFormat, dayOfWeekFormat);
var dateTimeFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter(hourFormat, minuteFormat, secondFormat);
var dateTimeFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter(yearFormat, monthFormat, dayFormat, dayOfWeekFormat, hourFormat, minuteFormat, secondFormat, iIterable(String);
var dateTimeFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter(yearFormat, monthFormat, dayFormat, dayOfWeekFormat, hourFormat, minuteFormat, secondFormat, iIterable(String);

Attributes

ActivatableAttribute(Windows.Globalization.DateTimeFormatting.IDateTimeFormatterFactory, NTDDI_WIN8)
DualApiPartitionAttribute()
MarshalingBehaviorAttribute(Agile)
StaticAttribute(Windows.Globalization.DateTimeFormatting.IDateTimeFormatterStatics, NTDDI_WIN8)
ThreadingAttribute(Both)
VersionAttribute(NTDDI_WIN8)

Members

The DateTimeFormatter class has these types of members:

Constructors

The DateTimeFormatter class has these constructors.

ConstructorDescription
DateTimeFormatter(HourFormat, MinuteFormat, SecondFormat) Creates a DateTimeFormatter object that is initialized with hour, minute, and second formats.
DateTimeFormatter(String) Creates a DateTimeFormatter object that is initialized by a format template string.
DateTimeFormatter(String, IIterable(String)) Creates a DateTimeFormatter object that is initialized by a format template string and language list.
DateTimeFormatter(String, IIterable(String), String, String, String) Creates a DateTimeFormatter object that is initialized by a format template string, language list, geographic region, calendar, and clock.
DateTimeFormatter(YearFormat, MonthFormat, DayFormat, DayOfWeekFormat) Creates a DateTimeFormatter object that is initialized with year, month, day, and day of week formats.
DateTimeFormatter(YearFormat, MonthFormat, DayFormat, DayOfWeekFormat, HourFormat, MinuteFormat, SecondFormat, IIterable(String)) Creates a DateTimeFormatter object that is initialized with year, month, day, day of week, hour, minute, and second formats, and a language list.
DateTimeFormatter(YearFormat, MonthFormat, DayFormat, DayOfWeekFormat, HourFormat, MinuteFormat, SecondFormat, IIterable(String), String, String, String) Creates a DateTimeFormatter object with all formats and identifiers specified.

 

Methods

The DateTimeFormatter class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
Format Returns a string representation of the provided date and time.

 

Properties

The DateTimeFormatter class has these properties.

PropertyAccess typeDescription

Calendar

Read-onlyGets the calendar that is used when formatting dates.

Clock

Read-onlyGets the clock that is used when formatting times.

GeographicRegion

Read-onlyGets or sets the region that is used when formatting dates and times.

IncludeDay

Read-onlyGets the DayFormat in the template.

IncludeDayOfWeek

Read-onlyGets the DayOfWeekFormat in the template.

IncludeHour

Read-onlyGets the HourFormat in the template.

IncludeMinute

Read-onlyGets the MinuteFormat in the template.

IncludeMonth

Read-onlyGets the MonthFormat in the template.

IncludeSecond

Read-onlyGets the SecondFormat in the template.

IncludeYear

Read-onlyGets the YearFormat in the template.

Languages

Read-onlyGets the priority list of language identifiers that is used when formatting dates and times.

LongDate

Read-onlyGets the DateTimeFormatter object that formats dates according to the user's choice of long date pattern.

LongTime

Read-onlyGets the DateTimeFormatter object that formats times according to the user's choice of long time pattern.

NumeralSystem

Read/writeGets or sets the numbering system that is used to format dates and times.

Patterns

Read-onlyGets the patterns corresponding to this template that are used when formatting dates and times.

ResolvedGeographicRegion

Read-onlyGets the geographic region that was most recently used to format dates and times.

ResolvedLanguage

Read-onlyGets the language that was most recently used to format dates and times.

ShortDate

Read-onlyGets the DateTimeFormatter object that formats dates according to the user's choice of short date pattern.

ShortTime

Read-onlyGets the DateTimeFormatter object that formats times according to the user's choice of short time pattern.

Template

Read-onlyGets a string representation of this format template.

 

Remarks

Note  If your app passes language tags used in this class to any National Language Support functions, it must first convert the tags by calling ResolveLocaleName.

Some of the constructors for DateTimeFormatter take a formatTemplate string parameter that specifies the requested components to be formatted. This parameter can be either a format template or a format pattern.

Format templates are strings that specify common date and time patterns. Valid templates are listed in the following table. In templates with more than one component, the component may be any of the supported component variations and the components may be in any order. For example, "dayofweek.full year month.numeric day" is supported.

Template
longdate
shortdate
longtime
shorttime
dayofweek
dayofweek.full
dayofweek.abbreviated
day
month
month.full
month.abbreviated
month.numeric
year
year.full
year.abbreviated
dayofweek day month year
dayofweek day month
day month year
day month
month year
hour
minute
second
hour minute second
hour minute
minute second

 

Format patterns are composed of a literal string with fields delimited by braces, such as "{day.numeric}/{month.numeric}/{year.full}". Valid fields are:

Format pattern field
{openbrace}
{closebrace}
{dayofweek.solo.<type>} where <type> is full / abbreviated / abbreviated(n)
{dayofweek.<type>} where <type> is full / abbreviated / abbreviated(n)
{day.<type>} where <type> is integer / integer(n)
{month.<type>} where <type> is full / abbreviated / abbreviated(n) / integer / integer(n)
{month.solo.<type>} where <type> is full / abbreviated / abbreviated(n)
{year.<type>} where <type> is full / abbreviated / abbreviated(n)
{era.<type>} where <type> is full / abbreviated / abbreviated(n)
{hour.<type>} where <type> is integer / integer(n)
{period.<type>} where <type> is full / abbreviated / abbreviated(n)
{minute.<type>} where <type> is integer / integer(n)
{second.<type>} where <type> is integer / integer(n)

 

Note  

  • Use {openbrace} or {closebrace} to specify that "{" or "}" appears as part of the literal string.
  • solo specifies the standalone form. For languages with genitive case distinctions, the genitive case is the unmarked case. To get the nominative case, use solo.
  • integer(2) for the fifth day of the month (for example) is "05", while integer(1) produces "5". integer produces the best n for a given language/calendar combination.
  • For years, full produces all digits ("2013"). abbreviated is synonymous with abbreviated(2), which is the 2-digit year for the gregorian calendar ("13"). (Other calendars may eventually use something else as the default; we have kept abbreviated to have the semantics of the best for a given language/calendar combination.) abbreviated(n) is truncated to the least significant n digits (for example, 2013 with n=1 is "3").

Examples

Use the DateTimeFormatter class to format a date.


// 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 mydatefmt = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("month day dayofweek year");
var dateToFormat = new Date();
var mydate = mydatefmt.format(dateToFormat);

Requirements

Minimum supported client

Windows 8 [Windows Store apps, desktop apps]

Minimum supported server

Windows Server 2012 [Windows Store apps, desktop apps]

Namespace

Windows.Globalization.DateTimeFormatting
Windows::Globalization::DateTimeFormatting [C++]

Metadata

Windows.winmd

See also

Date and time formatting sample

 

 

Build date: 12/4/2012

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.