Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Not A Problem
-
Adobe Flex SDK 3.2 (Release)
-
None
-
Affected OS(s): All OS Platforms
Affected OS(s): All OS Platforms
Language Found: English
Description
var df:DateFormatter = new DateFormatter();
df.formatString = "DD MMM YY"
trace(df.format(new Date(2000,1,1)))
You get 01 Feb 00 when you should get 01 Jan 00
The problem is in DateBase.extractTokenDate()
It looks up DateBase.monthNamesShort|monthNamesLong[month] when month is 1 based, not zero based like the array.
The case where (key < 3) realises this, and does month++
I'm using the Eclipse Plugin on MacOS.
This patch fixes it (use it in place of DateFormatter)
package
{
import mx.formatters.DateFormatter;
import mx.core.mx_internal;
import mx.managers.ISystemManager;
import mx.managers.SystemManager;
import mx.formatters.DateBase;
import mx.formatters.StringFormatter;
public class DateFormatterPatched extends DateFormatter
{
private static const VALID_PATTERN_CHARS:String = "Y,M,D,A,E,H,J,K,L,N,S";
public function DateFormatterPatched()
{ super(); } mx_internal static function extractTokenDate(date:Date,
tokenInfo:Object):String
{
//initialize();
//var result:String = "";
var key:int = int(tokenInfo.end) - int(tokenInfo.begin);
//var day:int;
//var hours:int;
switch (tokenInfo.token) {
case "M":
{
// month in year
var month:int = int(date.getMonth());
if (key < 3)
else if (key == 3)
{ return DateBase.monthNamesShort[month-1]; }else
{ return DateBase.monthNamesLong[month-1]; } }
}
return DateBase.mx_internal::extractTokenDate(date,tokenInfo)
}
override public function format(value:Object):String
{
// Reset any previous errors.
if (error)
error = null;
// If value is null, or empty String just return ""
// but treat it as an error for consistency.
// Users will ignore it anyway.
if (!value || value == "")
// – value –
if (value is String)
{
value = DateFormatter.parseDateString(String(value));
if (!value)
{ error = defaultInvalidValueError; return ""; }
}
else if (!(value is Date))
{ error = defaultInvalidValueError; return ""; }
// – format –
var letter:String;
var nTokens:int = 0;
var tokens:String = "";
var n:int = formatString.length;
for (var i:int = 0; i < n; i++)
{
letter = formatString.charAt;
if (VALID_PATTERN_CHARS.indexOf(letter) != -1 && letter != ",")
{
nTokens++;
if (tokens.indexOf(letter) == -1)
else
{
if (letter != formatString.charAt(Math.max(i - 1, 0)))
}
}
}
if (nTokens < 1)
{ error = defaultInvalidFormatError; return ""; } var dataFormatter:StringFormatter = new StringFormatter(
formatString, VALID_PATTERN_CHARS,
DateFormatterPatched.mx_internal::extractTokenDate);
//DateBase.mx_internal::extractTokenDate);
return dataFormatter.formatValue(value);
}
}
}