The DateFormatter class uses a format String to return a formatted date and time String from an input String or a Date object. You can create many variations easily, including international formats.
If an error occurs, an empty String is returned and a String describing
the error is saved to the error
property. The error
property can have one of the following values:
"Invalid value"
means a value that is not a Date object or a is not a recognized String representation of a date is passed to theformat()
method. (An empty argument is allowed.)"Invalid format"
means either theformatString
property is set to empty (""), or there is less than one pattern letter in theformatString
property.
The parseDateString()
method uses the feathers.formatters.DateBase class
to define the localized string information required to convert
a date that is formatted as a String into a Date object.
See also:
Static methods
staticparseDateString(str:String, ?format:String):Date
Converts a date that is formatted as a String into a Date object. Month and day names must match the names in feathers.formatters.DateBase.
The hour value in the String must be between 0 and 23, inclusive. The minutes and seconds value must be between 0 and 59, inclusive. The following example uses this method to create a Date object:
var myDate:Date = DateFormatter.parseDateString("2009-12-02 23:45:30");
The optional format property is use to work out which is likly to be encountered first a month or a date of the month for date where it may not be obvious which comes first.
Parameters:
str | Date that is formatted as a String. |
---|
Returns:
Date object.
See also:
Constructor
new(?formatString:String)
Constructor.
Parameters:
formatString | Date format pattern is set to this DateFormatter. |
---|
Variables
formatString:String
The mask pattern.
You compose a pattern String using specific uppercase letters, for example: YYYY/MM.
The DateFormatter pattern String can contain other text in addition to pattern letters. To form a valid pattern String, you only need one pattern letter.
The following table describes the valid pattern letters:
Y
Year. If the number of pattern letters is two, the year is truncated to two digits; otherwise, it appears as four digits. The year can be zero-padded, as the third example shows in the following set of examples:
- YY = 05
- YYYY = 2005
- YYYYY = 02005
M
Month in year. The format depends on the following criteria:
- If the number of pattern letters is one, the format is interpreted as numeric in one or two digits.
- If the number of pattern letters is two, the format is interpreted as numeric in two digits.
- If the number of pattern letters is three, the format is interpreted as short text.
- If the number of pattern letters is four, the format is interpreted as full text.
Examples:
- M= 7
- MM = 07
- MMM = Jul
- MMMM = July
D
Day in month. While a single-letter pattern string for day is valid, you typically use a two-letter pattern string.
Examples:
- D = 4
- DD = 04
- DD = 10
E
Day in week. The format depends on the following criteria:
- If the number of pattern letters is one, the format is interpreted as numeric in one or two digits.
- If the number of pattern letters is two, the format is interpreted as numeric in two digits.
- If the number of pattern letters is three, the format is interpreted as short text.
- If the number of pattern letters is four, the format is interpreted as full text.
Examples:
- E = 1
- EE = 01
- EEE = Mon
- EEEE = Monday
A
am/pm indicator.
J
Hour in day (0-23).
H
Hour in day (1-24).
K
Hour in am/pm (0-11).
L
Hour in am/pm (1-12).
N
Minute in hour.
Examples:
- N = 3
- NN = 03
S
Second in minute.
Example:
- SS = 30
Q
Millisecond in second
Example:
- QQ = 78
- QQQ = 078
O
Timezone offset
Example:
- O = +7
- OO = -08
- OOO = +4:30
- OOOO = -08:30
Z
Timezone name
Example:
- Z = GMT
Other text
You can add other text into the pattern string to further format the string. You can use punctuation, numbers, and all lowercase letters. You should avoid uppercase letters because they may be interpreted as pattern letters.
Example:
- EEEE, MMM. D, YYYY at L:NN:QQQ A = Tuesday, Sept. 8, 2005 at 1:26:012 PM
Methods
format(value:Dynamic):String
Generates a date-formatted String from either a date-formatted String or a Date object.
The formatString
property
determines the format of the output String.
If value
cannot be formatted, return an empty String
and write a description of the error to the error
property.
Parameters:
value | Date to format. This can be a Date object, or a date-formatted String such as "Thursday, April 22, 2004". |
---|
Returns:
Formatted String. Empty if an error occurs. A description
of the error condition is written to the error
property.