DateFormat Class
DateFormat is for formatting and parsing dates in a locale-sensitive manner. It allows the user to choose from a set of standard date time formats as well as specify a customized pattern under certain locales. Date elements that vary across locales include month name, week name, field order, etc. <!-- TODO(efortuna): Customized pattern system -- suggested by i18n needs feedback on appropriateness. --> We also allow the user to use any customized pattern to parse or format date-time strings under certain locales. Date elements that vary across locales include month name, weekname, field, order, etc.
This library uses the ICU/JDK date/time pattern specification as described below.
Time Format Syntax: To specify the time format use a time pattern string. In this pattern, following letters are reserved as pattern letters, which are defined in the following manner:
Symbol Meaning Presentation Example
------ ------- ------------ -------
G era designator (Text) AD
y# year (Number) 1996
M month in year (Text & Number) July & 07
d day in month (Number) 10
h hour in am/pm (1~12) (Number) 12
H hour in day (0~23) (Number) 0
m minute in hour (Number) 30
s second in minute (Number) 55
S fractional second (Number) 978
E day of week (Text) Tuesday
D day in year (Number) 189
a am/pm marker (Text) PM
k hour in day (1~24) (Number) 24
K hour in am/pm (0~11) (Number) 0
z time zone (Text) Pacific Standard Time
Z time zone (RFC 822) (Number) -0800
v time zone (generic) (Text) Pacific Time
' escape for text (Delimiter) 'Date='
'' single quote (Literal) 'o''clock'
Items marked with '#' work differently than in Java.
The count of pattern letters determine the format. Text: 4 or more pattern letters--use full form, less than 4--use short or abbreviated form if one exists. In parsing, we will always try long format, then short. (e.g., "EEEE" produces "Monday", "EEE" produces "Mon")
Number: the minimum number of digits. Shorter numbers are zero-padded to this amount (e.g. if "m" produces "6", "mm" produces "06"). Year is handled specially; that is, if the count of 'y' is 2, the Year will be truncated to 2 digits. (e.g., if "yyyy" produces "1997", "yy" produces "97".) Unlike other fields, fractional seconds are padded on the right with zero.
(Text & Number): 3 or over, use text, otherwise use number.
Any characters that not in the pattern will be treated as quoted text. For instance, characters like ':', '.', ' ', '#' and '@' will appear in the resulting time text even they are not embraced within single quotes. In our current pattern usage, we didn't use up all letters. But those unused letters are strongly discouraged to be used as quoted text without quote. That's because we may use other letter for pattern in future.
Examples Using the US Locale:
Format Pattern Result
-------------- -------
"yyyy.MM.dd G 'at' HH:mm:ss vvvv"->1996.07.10 AD at 15:08:56 Pacific Time
"EEE, MMM d, ''yy" ->Wed, July 10, '96
"h:mm a" ->12:08 PM
"hh 'o''clock' a, zzzz" ->12 o'clock PM, Pacific Daylight Time
"K:mm a, vvv" ->0:00 PM, PT
"yyyyy.MMMMM.dd GGG hh:mm aaa" ->01996.July.10 AD 12:08 PM
When parsing a date string using the abbreviated year pattern ("yy"), DateTimeParse must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time the parse function is called. For example, using a pattern of "MM/dd/yy" and a DateTimeParse instance created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" would be interpreted as May 4, 1964. During parsing, only strings consisting of exactly two digits, as defined by {@link java.lang.Character#isDigit(char)}, will be parsed into the default century. Any other numeric string, such as a one digit string, a three or more digit string will be interpreted as its face value.
If the year pattern does not have exactly two 'y' characters, the year is interpreted literally, regardless of the number of digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.
When numeric fields abut one another directly, with no intervening delimiter characters, they constitute a run of abutting numeric fields. Such runs are parsed specially. For example, the format "HHmmss" parses the input text "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and fails to parse "1234". In other words, the leftmost field of the run is flexible, while the others keep a fixed width. If the parse fails anywhere in the run, then the leftmost field is shortened by one character, and the entire run is parsed again. This is repeated until either the parse succeeds or the leftmost field is one character in length. If the parse still fails at that point, the parse of the run fails.
Constructors
Code new DateFormat.longDate([locale = '']) #
DateFormat.longDate([locale='']) : _locale = locale, _formatDefinition = longDate;
Code new DateFormat.Hms([locale = '']) #
DateFormat.Hms([locale='']) : _locale = locale, _formatDefinition = Hms;
Code new DateFormat.Md([locale = '']) #
DateFormat.Md([locale='']) : _locale = locale, _formatDefinition = Md;
Code new DateFormat.Hm([locale = '']) #
Constructors for dates/times that use a default format.
DateFormat.Hm([locale='']) : _locale = locale, _formatDefinition = Hm;
Code new DateFormat.fullDateTime([locale = '']) #
DateFormat.fullDateTime([locale='']) : _locale = locale, _formatDefinition = fullDateTime;
Code new DateFormat.MMM([locale = '']) #
DateFormat.MMM([locale='']) : _locale = locale, _formatDefinition = MMM;
Code new DateFormat.MMMMEd([locale = '']) #
DateFormat.MMMMEd([locale='']) : _locale = locale, _formatDefinition = MMMMEd;
Code new DateFormat.mediumTime([locale = '']) #
DateFormat.mediumTime([locale='']) : _locale = locale, _formatDefinition = mediumTime;
Code new DateFormat.yM([locale = '']) #
DateFormat.yM([locale='']) : _locale = locale, _formatDefinition = yM;
Code new DateFormat.hm([locale = '']) #
DateFormat.hm([locale='']) : _locale = locale, _formatDefinition = hm;
Code new DateFormat.yQQQ([locale = '']) #
DateFormat.yQQQ([locale='']) : _locale = locale, _formatDefinition = yQQQ;
Code new DateFormat.yMMMEd([locale = '']) #
DateFormat.yMMMEd([locale='']) : _locale = locale, _formatDefinition = yMMMEd;
Code new DateFormat.MEd([locale = '']) #
DateFormat.MEd([locale='']) : _locale = locale, _formatDefinition = MEd;
Code new DateFormat.fullTime([locale = '']) #
DateFormat.fullTime([locale='']) : _locale = locale, _formatDefinition = fullTime;
Code new DateFormat.MMMd([locale = '']) #
DateFormat.MMMd([locale='']) : _locale = locale, _formatDefinition = MMMd;
Code new DateFormat.MMMEd([locale = '']) #
DateFormat.MMMEd([locale='']) : _locale = locale, _formatDefinition = MMMEd;
Code new DateFormat([formatDefinition = fullDate, locale = '']) #
Constructor accepts a formatDefinition, which can be a String, one of the predefined static forms, or a custom date format using the syntax described above. An optional locale can be provided for specifics of the language locale to be used, otherwise, we will attempt to infer it (acceptable if Dart is running on the client, we can infer from the browser).
DateFormat([formatDefinition = fullDate, locale = '']) { this._formatDefinition = formatDefinition; this._locale = locale; }
Code new DateFormat.d([locale = '']) #
DateFormat.d([locale='']) : _locale = locale, _formatDefinition = d;
Code new DateFormat.ms([locale = '']) #
DateFormat.ms([locale='']) : _locale = locale, _formatDefinition = ms;
Code new DateFormat.mediumDate([locale = '']) #
DateFormat.mediumDate([locale='']) : _locale = locale, _formatDefinition = mediumDate;
Code new DateFormat.yMMM([locale = '']) #
DateFormat.yMMM([locale='']) : _locale = locale, _formatDefinition = yMMM;
Code new DateFormat.fullDate([locale = '']) #
DateFormat.fullDate([locale='']) : _locale = locale, _formatDefinition = fullDate;
Code new DateFormat.shortDateTime([locale = '']) #
DateFormat.shortDateTime([locale='']) : _locale = locale, _formatDefinition = shortDateTime;
Code new DateFormat.longTime([locale = '']) #
DateFormat.longTime([locale='']) : _locale = locale, _formatDefinition = longTime;
Code new DateFormat.shortDate([locale = '']) #
DateFormat.shortDate([locale='']) : _locale = locale, _formatDefinition = shortDate;
Code new DateFormat.yMMMM([locale = '']) #
DateFormat.yMMMM([locale='']) : _locale = locale, _formatDefinition = yMMMM;
Code new DateFormat.longDateTime([locale = '']) #
DateFormat.longDateTime([locale='']) : _locale = locale, _formatDefinition = longDateTime;
Code new DateFormat.y([locale = '']) #
DateFormat.y([locale='']) : _locale = locale, _formatDefinition = y;
Code new DateFormat.yQ([locale = '']) #
DateFormat.yQ([locale='']) : _locale = locale, _formatDefinition = yQ;
Code new DateFormat.M([locale = '']) #
DateFormat.M([locale='']) : _locale = locale, _formatDefinition = M;
Code new DateFormat.yMEd([locale = '']) #
DateFormat.yMEd([locale='']) : _locale = locale, _formatDefinition = yMEd;
Code new DateFormat.mediumDateTime([locale = '']) #
DateFormat.mediumDateTime([locale='']) : _locale = locale, _formatDefinition = mediumDateTime;
Code new DateFormat.shortTime([locale = '']) #
DateFormat.shortTime([locale='']) : _locale = locale, _formatDefinition = shortTime;
Code new DateFormat.MMMMd([locale = '']) #
DateFormat.MMMMd([locale='']) : _locale = locale, _formatDefinition = MMMMd;
Static Fields
Code final String Hm #
Date/Time format "skeleton" patterns. Also specifiable by String, but written this way so that they can be discoverable via autocomplete. These follow the ICU syntax described at the top of the file. These skeletons can be combined and we will attempt to find the best format for each locale given the pattern.
static final String Hm = 'Hm';
Methods
Code String format(Date date) #
Format the given date object according to preset pattern and current locale and return a formated string for the given date.
String format(Date date) { // TODO(efortuna): readd optional TimeZone argument (or similar)? return date.toString(); }
Code String formatDuration(Date reference) #
Returns a date string indicating how long ago (3 hours, 2 minutes) something has happened or how long in the future something will happen given a reference Date relative to the current time.
String formatDuration(Date reference) { return ''; }